代码之家  ›  专栏  ›  技术社区  ›  Suresh Mopidevi

如何使用Plist数据验证UItextfield中的用户输入

  •  0
  • Suresh Mopidevi  · 技术社区  · 6 年前

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

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

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

    目标C中

    2 回复  |  直到 6 年前
        1
  •  3
  •   Shehata Gamal    6 年前

    你可以试试

    敏捷的

    //强制展开以确保数据的存在

    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
    }
    

    目标-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
    }
    
        2
  •  0
  •   Sahm Zheii    6 年前

    获取plist是安全的

    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"]
            // ....
            // ....
        }