Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift equivalent to `initWithContentsOfFile:`?

Tags:

ios

swift

I am trying to use the Swift equivalent of initWithContentsOfFile: on an Array. The documentation states that the equivalent is convenience init(contentsOfFile aPath: String!)

I have tried to call this with the following code:

var path = NSBundle.mainBundle().pathForResource("Standards", ofType: "plist")
var rawStandards = Array<Dictionary<String, AnyObject?>>(contentsOfFile: path)

The compiler is telling me, however, that it couldn't find an overload that accepts the supplied arguments. What am I missing?

like image 550
Wayne Hartman Avatar asked Jun 10 '14 16:06

Wayne Hartman


2 Answers

@Leo Natan was really close, but the way that worked for me was:

var rawStandards = NSArray(contentsOfFile: path)

I was then able to access the elements:

for obj: AnyObject in rawStandards {
    if var rawStandard = obj as? NSDictionary {
    }
}
like image 114
Wayne Hartman Avatar answered Nov 14 '22 07:11

Wayne Hartman


You are initializing a Swift array, not an NSArray, which has the initWithContentsOfFile: initializer method.

Try:

var rawStandards = NSArray(contentsOfFile: path) as Dictionary<String, AnyObject?>[]
like image 37
Léo Natan Avatar answered Nov 14 '22 06:11

Léo Natan