【问题标题】:Unwrapping Plist and finding object展开 Plist 并找到对象
【发布时间】:2014-12-13 12:35:10
【问题描述】:

试图将目标 c 转换为 swift 感到非常沮丧。我有以下适用于目标 c 的代码。

NSMutableArray *path = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sequence List" ofType:@"plist"]];

//Shuffle the array of questions
numberSequenceList = [self shuffleArray:path];

currentQuestion = currentQuestion + 1;

if (Round==1) {
    //Take first object in shuffled array as the first question
    NSMutableArray *firstQuestion = [[NSMutableArray alloc] initWithArray:[numberSequenceList objectAtIndex:0]];

    //Find question and populate text view
    NSString *string = [firstQuestion objectAtIndex:0];

    self.lblNumber.text = string;

    //Find and store the answer
    NSString *findAnswer = [firstQuestion objectAtIndex:1];

    Answer = [findAnswer intValue];
}

但我似乎无法让它快速工作。我可以使用

拉出 plist 的内容
var path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist")

但我看不到在 swift 中有与 objectAtIndex 等价的东西。如果我尝试以下操作,我会收到一条错误消息,提示“字符串没有名为下标的成员”,这显然意味着我需要解开路径。

let firstQuestion = path[0]

【问题讨论】:

    标签: arrays swift plist


    【解决方案1】:

    您正在调用的方法(例如 NSBundle.mainBundle().pathForResource)返回可选项,因为它们可能会失败。在 Objective-C 中,该失败由 nil 指示,而 Swift 使用可选项。

    所以在你的例子中:

    var path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist")
    

    pathOptional<String>(或 String?)类型,而不是 String 类型。 Optional<String> 没有下标方法(即不支持 [ ])。要在其中使用字符串,您必须检查可选项是否包含值(即对 pathForResource 的调用成功):

    // the if let syntax checks if the optional contains a valid 
    if let path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist”) {
        // use path, which will now be of type String
    }
    else {
        // deal with pathForResource failing
    }
    

    您可以在the Swift book 的介绍中阅读更多关于可选的信息。

    【讨论】:

    • 您好,感谢您的解释。但是,当我尝试使用 if 函数时,我现在收到一条消息,提示“下标不可用:不能用 int 下标字符串”。如何使用字符串中的第一个对象?
    • first(mystring) (再次返回一个可选值,因为字符串可能为空)。或者mystring[mystring.startIndex]——字符串不是整数索引的,因为它们不能保证是随机访问的(因为某些字符可以是不同的字节长度)
    【解决方案2】:

    您还没有翻译 Objective-C 的整个第一行。您错过了对 NSMutableArray 的调用,它根据文件的内容创建数组。原始代码令人困惑,因为它在真正有问题时会调用文件path 的内容。试试这个:

    if let path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist") {
        let questions = NSMutableArray(contentsOfFile: path)
    
        //Shuffle the array of questions
        numberSequenceList = self.shuffleArray(questions)
    
        currentQuestion = currentQuestion + 1
    
        if Round == 1 {
            //Take first object in shuffled array as the first question
            let firstQuestion = numberSequenceList[0] as NSArray
    
            //Find question and populate text view
            let string = firstQuestion[0] as NSString
    
            self.lblNumber.text = string
    
            //Find and store the answer
            let findAnswer = firstQuestion[1] as NSString
    
            Answer = findAnswer.intValue
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2021-04-05
      • 2019-11-18
      • 1970-01-01
      • 2021-07-10
      • 2020-12-27
      相关资源
      最近更新 更多