简体   繁体   English

Swift 3核心数据获取问题

[英]Swift 3 Core Data fetch issue

The function below is used to get data for tableview 下面的函数用于获取tableview的数据

func fetchProfiles() -> Array<User> {
    var users: Array<User> = []
    let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
    let fetchedData = try! context.fetch(fetchRequest)
    if (!fetchedData.isEmpty) {
        print(fetchedData)
        for i in 0...fetchedData.count {
            var user: User = User()
            user.userName = fetchedData[i].profileName
            user.userSurname = fetchedData[i].profileSurname
            user.userPhoto = fetchedData[i].profilePhoto
            users.append(user)
        }
        return users
    }
    else {
        return users
    }
}

"User" is a simple struct. “用户”是一个简单的结构。 "Profile" is an entity in Core Data. “Profile”是Core Data中的一个实体。 I create an array of structs to use them for cells in table. 我创建了一个结构数组,用于表中的单元格。 Code has no errors(for xCode). 代码没有错误(对于xCode)。 When there is no fetched data, it skips appending array, but when there is some info, app crashes with error: 当没有提取数据时,它会跳过附加数组,但是当有一些信息时,应用程序崩溃并显示错误:

fatal error: NSArray element failed to match the Swift Array Element type 致命错误:NSArray元素无法匹配Swift数组元素类型

For unknown reason, there were problems with auto generated headers of Core Data entities. 由于未知原因,自动生成的Core Data实体标头存在问题。 I just deleted all files from folder: 我刚从文件夹中删除了所有文件:

/Users/user/Library/Developer/Xcode/DerivedData/MyApp/Build/Intermediates/MyApp.build/Debug-iphonesimulator/MyApp.build/DerivedSources/CoreDataGenerated /Users/user/Library/Developer/Xcode/DerivedData/MyApp/Build/Intermediates/MyApp.build/Debug-iphonesimulator/MyApp.build/DerivedSources/CoreDataGenerated

then cleaned my project with 然后清理我的项目

command+shift+k 命令+移+ K

checked my core data model for some possible issues and rebuilt the whole project. 检查了我的核心数据模型是否存在一些可能的问题,并重建了整个项目。 Magic. 魔法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM