【问题标题】:how to globally used parsed values in swift如何在swift中全局使用解析值
【发布时间】:2016-05-04 08:06:20
【问题描述】:
func jsonParsing1(){
           do{
            let path : NSString =  NSBundle.mainBundle().pathForResource("fileName", ofType: "json")!
            let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMappedIfSafe)
            let jsonData = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
            let jsonArray = jsonData
           ** let templeArray = (jsonArray.valueForKey("temple-name") as? NSArray)!**

           }catch {
           print(error)


        }
    }
}

我的 json 文件是 [ { “寺庙名称”:“aaa”, “图像”:“图像.png”, “描述”:“aaa” }, { “寺庙名称”:“bbb”, “图像”:“图像1.png”, “描述”:“bbb” } ] 我在一个单独的类中使用 json 文件,并尝试通过项目访问解析的数组。 使用全局数组,但从另一个类调用时返回 nil。提前致谢。

我需要全局使用 TempleArray。

【问题讨论】:

    标签: ios json swift global-variables


    【解决方案1】:

    您可以使用 TempleArray 属性创建一个单例类,将值存储在此数组中,并通过单例类的共享实例访问它。 或者,

    您可以在 appDelegate 中声明 TempleArray 属性并对其进行全局访问。

    【讨论】:

      【解决方案2】:

      您可以通过static 变量访问项目中的数据。

      class GlobalVariables {
          static var templeArray : NSMutableArray?
      }
      

      您可以通过

      在整个应用程序中使用templeArray
      GlobalVariables.templeArray?.objectAtIndex(index)
      

      【讨论】:

        【解决方案3】:

        这段代码应该可以工作:

        var templeArray: NSMutableArray = []
        
        class Parser {
        
            func jsonParsing1() {
                do {
                    let path : NSString =  NSBundle.mainBundle().pathForResource("fileName", ofType: "json")!
                    let data : NSData = try NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMappedIfSafe)
                    let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
                    let jsonArray = jsonData
                    if let rawTempleArray = jsonArray.valueForKey("temple-name") as? NSMutableArray {
                        templeArray = rawTempleArray
                    } else {
                        print("temple-name is not array")
                    }
                } catch {
                    print(error)
                }
            }
        }
        

        我会这样做:

        struct Temple { //or class
            // your fields here
            init? (templeDictionary: NSDictionary) {
                // your parser here
            }
        }
        
        
        class TheParser {
            static var theTempleArray: [Temple] = []
        
            func jsonParsing1() {
                do {
                    let path : NSString =  NSBundle.mainBundle().pathForResource("fileName", ofType: "json")!
                    let data : NSData = try NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMappedIfSafe)
                    let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
                    let jsonArray = jsonData
                    if let rawTempleArray = jsonArray.valueForKey("temple-name") as? NSArray {
                        for temple in rawTempleArray {
                            if let theTemple = temple as? NSDictionary { // or NSArray or String depending of your json structure
                                if let templeItem = Temple(templeDictionary: theTemple) {
                                    TheParser.theTempleArray.append(templeItem)
                                }
                            }
                        }
                    } else {
                        print("temple-name is not array")
                    }
        
                } catch {
                    print(error)
        
                }
            }
        
        }
        

        【讨论】:

          【解决方案4】:

          请尝试这种方法:

          class MyClass {
          
          static var templeArray : NSArray?
          
          
               func jsonParsing1(){
                  let path : NSString =  NSBundle.mainBundle().pathForResource("fileName", ofType: "json")!
                  let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMappedIfSafe)
                  let jsonData = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
                  let jsonArray = jsonData
                  MyClass.templeArray = (jsonArray.valueForKey("temple-name") as? NSArray)!
          
                  /*
                   * You can now access to templeArray in this way:
                   * let myArray : NSArray? = MyClass.templeArray
                   */
               }
          }
          

          另外,do/catch 块在 Swift2 中是没用的。

          编辑: 你做错了。

          let templeArray = (jsonArray.valueForKey("temple-name") as? NSArray)!
          

          将始终为 nil,因为字段 Temple name 不是数组,而是字符串。 你必须这样做:

          MyClass.templeArray = jsonArray
          

          那么你可以这样进入第一座寺庙:

          let temple = MyClass.templeArray[0]
          let templeName : String = temple.objectForKey("temple-name") as? String
          

          【讨论】:

          • 那是因为你正在执行条件向下转换,所以你确定你的 json 中有数据吗?
          • 是的,当我使用 jsonParsing() 打印 TempleArray 时,它会打印值。但在函数之外,它显示为 nil。
          • 能否请您提供一个 sn-p 以查看您是如何访问阵列的?
          • 如图,class ListView: UITableViewController { override func viewDidLoad() { super.viewDidLoad() TempleRows = MyClass.templeArray }
          • 请用json示例更新您的初始帖子,解析json时的点和访问数组的点。
          猜你喜欢
          • 1970-01-01
          • 2011-08-05
          • 2019-01-02
          • 2018-02-10
          • 2021-03-02
          • 2020-08-24
          • 1970-01-01
          • 2022-08-21
          • 2021-11-02
          相关资源
          最近更新 更多