當我嘗試通過使用外部數據庫與我的應用程序上的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