Using Swift, how can I tell the difference between boolean and integer value when loading data from plist?

My application needs to be able to read the plist file and know the data type, so I created a function to determine the type of the object:

func getTypeOfObject(object: AnyObject) -> String {
    if object as? [String:AnyObject] != nil {
        return "dict"
    } else if object as? [AnyObject] != nil {
        return "array"
    } else if object as? Bool != nil {
        return "bool"
    } else if object as? Int != nil {
        return "int"
    } else if object as? String != nil {
        return "string"
    } else {
        return ""
    }
}

      

The problem is, if I call the function on a number, I get "bool". If I change the function to check the number first, I get "int" when I pass in a boolean.

This is because when I create a dict from data in a plist file, booleans gets an integer value (1 for true and 0 for false).

Is there a way to get around this?

+3


source to share


2 answers


I did some more research and decided to use CFPropertyList

which solves my problem since booleans are CFBooleanRef

s and not NSNumber

s.



If anyone wants the actual code to be requested in the comments. This is a little messy right now, so I don't post it right away.

0


source


You can't - unless you want to parse the XML plist file yourself. The return value will be NSNumber and it's up to you if you want to treat it as a bool (via NSNumber boolValue).



+4


source







All Articles