【问题标题】:How to validate user input from UItextfield with Plist data如何使用 Plist 数据验证来自 UItextfield 的用户输入
【发布时间】:2018-05-16 10:26:14
【问题描述】:

我有一个包含用户名和密码的 Plist 文件。

  • 如何在点击提交按钮时从 Plist 中获取数据并使用用户输入值进行验证。

  • 按下提交按钮后。如果用户输入数据与 Plist 中的数据匹配。它应该显示登录成功并在另一个视图控制器中显示配置文件数据。

在 Objective-C 中

【问题讨论】:

    标签: ios objective-c uibutton plist


    【解决方案1】:

    你可以试试

    斯威夫特

    // 强制解包以确保数据存在

    let path = Bundle.main.path(forResource: "fileName", ofType: "plist")  
    
    let dic = NSDictionary(contentsOfFile: path) as! [String:String]  
    
    self.name = dic["username"] as! String
    
    self.pass = dic["password"] as! String
    

    提交时

    检查

    if username.text == name && password.text == pass {
        // success
    }
    

    Objective-C

    NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"]];
    
    NSString *name = mainDictionary[@"username"];
    
    NSString *pass = mainDictionary[@"password"];
    
    if ([username.text isEqualToString:name] && [password.text isEqualToString:pass]) {
    
         // success
    }
    

    【讨论】:

    • 我正要发布答案,而你刚刚发布了它:) +1 为它
    • @PPL thnks 下次会为您保留它=D
    【解决方案2】:

    get plist is safe by

    func getDictOfPlist(fileName : String) -> [String:String]? {
        guard let filePath = Bundle.main.path(forResource: fileName, ofType: "plist") else {
            // can't gen file path
            return nil
        }
        guard let dict = NSDictionary(contentsOfFile: filePlist) as? [String:String] else {
            // can't read file or data type false
            return nil
        }
        return dict
    }
    

    用在

        if let content = getDictOfPlist(fileName: "UserData") {
            username.text = content["name"]
            // ....
            // ....
        }
    

    【讨论】:

    • 问题被标记为objective-c
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多