如果从nsdictionary字符串为空,如何获取数组计数?

时间:2019-02-05 13:18:30

标签: swift

这是我的回应

{

    activeFromDate = "01/01/2017";
    terminateDate = “02/05/2019”;

}

{

    activeFromDate = "01/01/2013";
    terminateDate = "<null>";

}

{

    activeFromDate = "01/01/2017";
    terminateDate = "02/05/2018";

}

{

    activeFromDate = "07/01/2012";
    terminateDate = "<null>";

}

{

    activeFromDate = "01/01/2017";
    terminateDate = "02/05/2019";

}

这是我的代码

 let answerArray = NSMutableArray()

    for i in 0..<arr.count
    {
        let dict = Dictionary[i] as! NSDictionary
        let name1 = dict.value(forKey:"terminateDate") as! String
        if(name1 == "")
        {
            print (answerArray.add(dict))
        }
    }

这是我的必填项 如果terminalDate为null并且terminateDate大于今天,该如何获取数组计数?

1 个答案:

答案 0 :(得分:0)

我创建了一个示例来读取放置在您的项目中的json文件。并已根据您的要求集成了代码。

Json文件

{"Status":"success","Data":[
    {"activeFromDate":"01/01/2017","terminateDate":"02/05/2019"},
    {"activeFromDate":"01/01/2013","terminateDate":null},
    {"activeFromDate":"01/01/2017","terminateDate":"02/05/2018"},
    {"activeFromDate":"07/01/2012","terminateDate":null},
    {"activeFromDate":"01/01/2017","terminateDate":"02/05/2016"},
    {"activeFromDate":"01/01/2017","terminateDate":"02/05/2019"}
]
}

在您的View Controller中

import UIKit

class HomeViewController: UIViewController {

let answerArray = NSMutableArray()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    jsonParsingFromFile()
}


func jsonParsingFromFile()
{
    let path: NSString = Bundle.main.path(forResource: "tjw", ofType: "json")! as NSString
    let data : NSData = try! NSData(contentsOfFile: path as String, options: NSData.ReadingOptions.dataReadingMapped)
    self.startParsing(data: data)
}

func startParsing(data :NSData)
{
    let dict: NSDictionary!=(try! JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary
    //print(dict)
    guard let dataArr:NSArray = dict.value(forKey: "Data") as? NSArray else {
        return
    }
    print(dataArr.count)
    for i in 0..<dataArr.count{
        guard let currentDict:NSDictionary = dataArr[i] as? NSDictionary else {
            return
        }
        let activeFromDateStr:String = currentDict.value(forKey: "activeFromDate") as? String ?? ""
        let terminateDateStr:String = currentDict.value(forKey: "terminateDate") as? String ?? ""
        print(activeFromDateStr, terminateDateStr)
        if terminateDateStr != ""{
            let date2 = convertToDate(dateStr: terminateDateStr)
            let today = Date()
            if date2>today{
                answerArray.add(currentDict)
            }

        }else{
            answerArray.add(currentDict)
        }
    }
    print(answerArray)

}

func convertToDate(dateStr:String)->Date{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/MM/yyyy"
    dateFormatter.locale = Locale(identifier: "en_IN")
    let date = dateFormatter.date(from: dateStr)!
    return date
}


}