如何在swift中编写SFHFKeychainUtils

时间:2017-06-12 12:23:43

标签: ios swift

如何在swift 3.0中编写此代码

-(void)keychainitems
{
 NSString *appidStr;
NSError *saveerror;

NSString *savedstring=[SFHFKeychainUtils getPasswordForUsername:@"vikramarka" andServiceName:@"com.vikram.vikramarka" error:&saveerror ];
NSString *tempuniqstr;
if (savedstring==nil || savedstring.length<1)
{
    NSUUID *uuid = [[NSUUID alloc] init];
    tempuniqstr=[uuid UUIDString];

[SFHFKeychainUtils storeUsername:@"vikramarka" andPassword:tempuniqstr forServiceName:@"com.vikram.vikramarka" updateExisting:YES error:&saveerror ];  

  appidStr = tempuniqstr;

else
{
    appidStr = savedstring;
}

1 个答案:

答案 0 :(得分:1)

func keychainitems() {
    var appidStr: String
    var saveerror: Error?
    let savedstring: String? = try? SFHFKeychainUtils.getPasswordForUsername("vikramarka", andServiceName: "com.vikram.vikramarka")
    var tempuniqstr: String
    if savedstring == nil || (savedstring?.characters.count ?? 0) < 1 {
        let uuid = UUID()
        tempuniqstr = uuid.uuidString
        try? SFHFKeychainUtils.storeUsername("vikramarka", andPassword: tempuniqstr, forServiceName: "com.vikram.vikramarka", updateExisting: true)
        appidStr = tempuniqstr
    }
    else {
        appidStr = savedstring
    }
}

使用this utility将未来的目标C代码转换为swift 3