【发布时间】:2015-09-24 19:28:44
【问题描述】:
您好,我是 swift 的新手,我正在尝试创建一个可选的数组,有时它为空,我不知道正确的语法是什么,这是代码
public final class Content: ResponseObject,ResponseCollection {
public let created: String
public let name: String
public let children: [Content]?
@objc required public init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.name = representation.valueForKeyPath("name") as! String
self.created = representation.valueForKeyPath("created") as! String
self.children = Content.collection(response:response, representation: representation.valueForKeyPath("children")!)
}
@objc public static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Content] {
var contents: [Content] = []
if let representation = representation as? [[String: AnyObject]] {
for contentRepresentation in representation {
if let content = Content(response: response, representation: contentRepresentation) {
contents.append(content)
}
}
}
return contents
}
children 有时可以为 nil,但为 null 时会崩溃。
【问题讨论】:
标签: ios iphone swift alamofire