简体   繁体   中英

How to get the 'expected' type of a CoreData attribute in Swift?

I am writing an app in iOS SDK 8.4 in Xcode Version 6.4 (6E35b) using Swift. I have a .xcdatamodeld file that has an entity named 'Assessment'. The 'Assessment' entity has several attributes of types: String, Bool, and Float. When I check via the attributeType accessor in NSAttributeDescription as suggested in many posts, I receive a nil value which makes sense, because I'm using an instance which has not mapped all values to the attributes.

From apple's reference: "The NSAttributeDescription class is used to describe attributes of an entity described by an instance of NSEntityDescription."

How do I get the type of an attribute as I defined in the .xcdatamodeld file programmatically?

Know this is old but wanted to update it with an answer of how I am doing it in Swift 4:

let attributeDescription = myDataSource.entity.attributesByName

    for thisDescription in attributeDescription {
        if thisDescription.1.attributeType == .integer16AttributeType {
            print(thisDescription.0)
        }
    }

Where myDataSource is a NSManagedObject I fetched from Core Data. In my example I was looking to make a grid with the max col being the highest int value of my managed object. attributeDescription is a tuple, 0 provides the string title you gave to an attribute, 1 is a dictionary of values of the attribute.

You can view all the attributeTypes here:

https://developer.apple.com/documentation/coredata/nsattributetype

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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