使用 PropertyListSerialization 在 swift 中创建字典 2
Use PropertyListSerialization to Make a Dictionary in swift 2
我正在尝试在 swift 2 中使用 PropertyListSerialization 制作字典。但它不断出现此错误:
"can not convert value of type 'Int' to expected argument type 'NSPropertyListReadOptions'
我的代码行是:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in
if data != nil {
let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil)
还有这个:
let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil)
然后我必须遍历字典:
for(key, value): (AnyObject, AnyObject) in datasourceDictionary {
let name = key as? String
let url = NSURL(string:value as? String ?? "")
if name != nil && url != nil {
let photoRecord = PhotoRecord(name:name!, url:url!)
self.photos.append(photoRecord)
}
}
您不需要将其更改为 Int:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in
if data != nil {
let datasourceDictionary = try! NSPropertyListSerialization
.propertyListWithData(data!,
options:NSPropertyListMutabilityOptions.Immutable,
format: nil)
还有这个:
let datasourceDictionary = try! NSPropertyListSerialization
.propertyListWithData(data! ,
options:NSPropertyListMutabilityOptions.Immutable,
format: nil)
您应该解包您的数据对象,而不是根据 nil 检查它。你还需要实现 do try catch 错误处理而不是强制它:
if let data = data {
do {
let datasourceDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: .MutableContainersAndLeaves, format: nil)
print(datasourceDictionary)
} catch let error as NSError {
print(error.code, error.domain)
}
}
我正在尝试在 swift 2 中使用 PropertyListSerialization 制作字典。但它不断出现此错误:
"can not convert value of type 'Int' to expected argument type 'NSPropertyListReadOptions'
我的代码行是:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in
if data != nil {
let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil)
还有这个:
let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil)
然后我必须遍历字典:
for(key, value): (AnyObject, AnyObject) in datasourceDictionary {
let name = key as? String
let url = NSURL(string:value as? String ?? "")
if name != nil && url != nil {
let photoRecord = PhotoRecord(name:name!, url:url!)
self.photos.append(photoRecord)
}
}
您不需要将其更改为 Int:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in
if data != nil {
let datasourceDictionary = try! NSPropertyListSerialization
.propertyListWithData(data!,
options:NSPropertyListMutabilityOptions.Immutable,
format: nil)
还有这个:
let datasourceDictionary = try! NSPropertyListSerialization
.propertyListWithData(data! ,
options:NSPropertyListMutabilityOptions.Immutable,
format: nil)
您应该解包您的数据对象,而不是根据 nil 检查它。你还需要实现 do try catch 错误处理而不是强制它:
if let data = data {
do {
let datasourceDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: .MutableContainersAndLeaves, format: nil)
print(datasourceDictionary)
} catch let error as NSError {
print(error.code, error.domain)
}
}