簡體   English   中英

使用NSArray和NSDictionary:無法將“字符串”類型的值轉換為預期的參數類型“ int”

[英]Using NSArray and NSDictionary: Can not convert value of type “string” to expected argument type “int”

我正在學習使用mysql + php的外部數據庫。 數據結構非常簡單。 我只有1個表“ post”,具有4個屬性:id,標題,作者和內容。 然后我嘗試從在線視頻中學習

如果我復制了正確的代碼,則可以成功構建,后來出現錯誤消息

“無法將類型'__NSArrayM'(0x882178)的值強制轉換為'NSDictionary'(0x882394)。

在編碼

let response = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary". 

因此,我在編碼中將NSDictionary更改為NSArray ,然后在另一種編碼中出現了另一個錯誤,甚至無法成功構建。

“無法將'String'類型的值轉換為預期的參數“ int”

在編碼

self.loadPosts(response["posts"] as! NSArray)

現在我不該做什么,請幫忙。

這些是郵政service.swift的編碼service.swift

import Foundation

class PostService {
   var settings:Settings

   init () {
   self.settings = Settings()

   }

   func getPosts(callback:(NSArray) -> () ){

        request(settings.viewPosts, callback: callback)
   }

    func request(url:String, callback:(NSArray) -> ()){

        let nsURL = NSURL(string: url)

        let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!)
            {(data,response,error) in

                let _: NSError

                let response = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
                callback (response)
         }

        task.resume()
      }
}

以下代碼位於masterviewcontroller.swift

var detailViewController: DetailViewController? = nil
var postsCollection = [Post]()
var service:PostService!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem()

    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
    self.navigationItem.rightBarButtonItem = addButton
    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }
    service = PostService()
    service.getPosts{
        (response) in
        self.loadPosts(response["posts"] as! NSArray)
    }


}

// func loadPosts
func loadPosts (posts:NSArray){

    for post in posts {
        let id = post["Post"]!!["id"] as! String
        let title = post["Post"]!!["title"] as! String
        let author = post["Post"]!!["author"] as! String
        let content = post["Post"]!!["content"] as! String
        let postObj = Post(id: id, title: title, author: author, content: content)
        postsCollection.append(postObj)
        dispatch_async(dispatch_get_main_queue()){
            self.tableView.reloadData()
        }


    }

}

我找到了解決方案。 我只是將self.loadPosts(response [“ posts”]作為NSArray)更改為以下內容,並且可以正常工作。

self.loadPosts(response)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM