How to create a NSMutableDictionary in Swift? -
i'm trying create nsmutabledictionary use key chain in ios, i'm getting following error.
could not find overload 'init' accepts supplied arguments
this code i'm using:
var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpassword, service, useraccount, kcfbooleantrue, ksecmatchlimitone], forkeys: [ksecclass, ksecattrservice, ksecattraccount, ksecreturndata, ksecmatchlimit])
any idea causing this?
the complete code:
import uikit import security let serviceidentifier = "myserivice" let useraccount = "authenticateduser" let accessgroup = "myserivice" class keychainservice: nsobject { class func savetoken(token: nsstring) { self.save(serviceidentifier, data: token) } class func loadtoken() -> nsstring? { var token = self.load(serviceidentifier) return token } class func save(service: nsstring, data: nsstring) { var datafromstring: nsdata = data.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) // instantiate new default keychain query var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpassword, service, useraccount, datafromstring], forkeys: [ksecclass, ksecattrservice, ksecattraccount, ksecvaluedata]) // delete existing items secitemdelete(keychainquery cfdictionaryref) // add new keychain item var status: osstatus = secitemadd(keychainquery cfdictionaryref, nil) } class func load(service: nsstring) -> nsstring? { // instantiate new default keychain query // tell query return result // limit our results 1 item var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpassword, service, useraccount, kcfbooleantrue, ksecmatchlimitone], forkeys: [ksecclass, ksecattrservice, ksecattraccount, ksecreturndata, ksecmatchlimit]) var datatyperef :unmanaged<anyobject>? // search keychain items let status: osstatus = secitemcopymatching(keychainquery, &datatyperef) let opaque = datatyperef?.toopaque() var contentsofkeychain: nsstring? if let op = opaque? { let retrieveddata = unmanaged<nsdata>.fromopaque(op).takeunretainedvalue() // convert data retrieved keychain string contentsofkeychain = nsstring(data: retrieveddata, encoding: nsutf8stringencoding) } else { println("nothing retrieved keychain. status code \(status)") } return contentsofkeychain } }
for code can use nsdictionary way:
var keychainobjects = nsarray(objects:"\(ksecclassgenericpassword)", service, useraccount, datafromstring) var keychainvalues = nsarray(objects:"\(ksecclass)","\(ksecattrservice)", "\(ksecattraccount)","\(ksecvaluedata)") let keychainquery = nsdictionary(objects: keychainobjects, forkeys: keychainvalues)
Comments
Post a Comment