当我尝试通过使用外部数据库与我的应用程序上的API连接数据库时,出现loadPostsfunction
错误。我该如何解决这个错误“AnyObject?不能转换为'字符串'”?
错误:
AnyObject is not convertible to String
代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
service = PostService()
service.getPosts(){
(response) in
self.loadPosts(response["posts"]! as! NSArray)
}
}
/////////////////// In here ///////////////////////
func loadPosts(posts:NSArray) {
for post in posts {
var id = (post["Post"]!["id"]! as! String).toInt()!
var title = post["Post"]!["title"]! as String
var author = post["Post"]!["author"]! as String
var content = post["Post"]!["content"]! as String
}
}
////////////////////////////////////////////////////
//Link DB
var postsCollection = [Post]()
var service:PostService!
}
任何想法吗?
欢迎来到StackOverflow!如果你能提供更多的细节,你的问题可能会更容易分析:你会得到哪条线错误? 'response [“posts”]'包含什么样的数据,我认为它是一个'NSDictionary'数组,这是正确的吗?你能用这些细节更新你的问题,谢谢? – Cristik