docs
和撇除
this article
,我认为url属性不应该指向文件。相反,它应该:
我想正确的方法是
在URL中编码应用程序数据。例如,您可以将数据编码为URL查询字符串中的键值对,如下所示:
guard let components = NSURLComponents(string: myBaseURL) else {
fatalError("Invalid base url")
}
let size = NSURLQueryItem(name: "Size", value: "Large")
let count = NSURLQueryItem(name: "Topping_Count", value: "2")
let cheese = NSURLQueryItem(name: "Topping_0", value: "Cheese")
let pepperoni = NSURLQueryItem(name: "Topping_1", value: "Pepperoni")
components.queryItems = [size, count, cheese, pepperoni]
guard let url = components.url else {
fatalError("Invalid URL components.")
}
message.url = url
(代码取自文档,您可能希望将其转换为ObjC…)
因此,与其将字典转换为NSData并将其写入文件,不如将其编码为queryItems,可能如下所示:
NSMutableArray *queryItems = [[NSMutableArray alloc] init]; // Or initWithCapacity for the sake of performance...
NSDictionary *dict = [Converter toDictionary:array];
for (id key in dict) {
id value = queryDictionary[key];
NSURLQueryItem *queryItem = [NSURLQueryItem queryItemWithName:key value:value];
[queryItems addObject:queryItem];
}
[url setQueryItems:queryItems];