我使用的 API 以简洁的格式为我提供数据 - 如何将其作为String. 我可以打印它,但将其保存为字符串似乎不起作用。
我特别想使用NSURLSession任务中的数据更新 UI 元素。
let url = NSURL(string: apiCall)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
//I want to replace this line below with something to save it to a string.
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
Run Code Online (Sandbox Code Playgroud) 在尝试寻找解决方案时,我遇到了这个答案:
它似乎实现了我想要的目标,但在目标 C 中:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)
我如何快速做到这一点?我的第一次尝试返回错误:
无法转换表达式的类型“$T4??” 输入“UITabBarController”
var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1
Run Code Online (Sandbox Code Playgroud) 用这段代码告诉我 'String' is not convertible to 'NSMutableAttributedString'
var textViewString:NSMutableAttributedString = ""
如何将此字符串正确初始化为空?(当我遍历一个将项目附加到字符串的循环时,它需要为空.)