gpt4 book ai didi

ios - Objective-C + Swift with CoreData - 无法将 'EntityName' 的值类型分配给类型 'EntityName!'

转载 作者:行者123 更新时间:2023-11-30 12:50:11 24 4
gpt4 key购买 nike

SENARIO

我正在开发一个项目,将 Swift 3 合并到 Objective-C 项目中。该项目正在使用 CoreData,在我的 Objective-C AppDelegate 中,我声明了“EntityName”的类型,并尝试在我的 Swift 3 类中进行分配,但收到构建错误“无法分配 ' 的类型” EntityName' 来输入“EntityName!”。

<小时/>

代码

AppDelegate.h

// Declaration
@property (strong, nonatomic) EntityName * entity;

Swift 类

// Array Property
var entityList = [EntityName]()

...

// Assignment
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.entity = self.entityList[indexPath.row] // throws error
<小时/>

错误

can not assign type of 'EntityName' to type 'EntityName!'
<小时/>

我尝试过的

1)

appDelegate.entity = self.entityList[indexPath.row] as EntityName! // throws error

错误

can not assign type of 'EntityName!' to type 'EntityName!'

2)

appDelegate.entity = self.entityList[indexPath.row] as EntityName? // throws error

错误

can not assign type of 'EntityName?' to type 'EntityName!'
<小时/>

问题

我该如何纠正这个问题并继续我的一天?

最佳答案

经过一番尝试和错误后,我找到了解决此问题的方法。就我而言,我必须遵循以下步骤:

1)导航到并选择 CoreData 模型...

AppName.xcdatamodeld

2) 在右侧,查看文件检查器...

enter image description here

3)代码生成部分,选择 Objective-C 作为语言...

enter image description here

<小时/>

我的 .xcdatamodeld 文件代码生成设置为 Swift。我相信这句老话“让 Swift 符合 Objective-C 比 Objective-C 符合 Swift 更容易”在这里是正确的。

关于ios - Objective-C + Swift with CoreData - 无法将 'EntityName' 的值类型分配给类型 'EntityName!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com