2016-11-16 5 views
0

Je suis en train de créer JSON Token Web à l'aide JSONSerialization classe, Swift 3 et Xcode 8.1, mais mon projet échoue avec l'erreur:Convertir Dictionnaire base64: erreur Segmentation faute 11

Command failed due to signal: Segmentation fault 11.

Tout le monde sait pourquoi mon le code n'est pas correct?

Si je commente ce code à partir du projet, le projet se construit.

let customerError = "Custom Error" 
enum headerError: Error { 
case customerError 
} 

let headerJWT: [Dictionary] = ["alg":"RS256","typ":"JWT"] 

//Convert headerJWT to Data 
do { 
    let headerJWTData: Data = try? JSONSerialization.data(withJSONObject:headerJWT,options: JSONSerialization.WritingOptions.prettyPrinted) 
} catch headerError.customerError { 
     print("could not make data") 
} 

//Convert headerData to string utf8 
do { 
    let headerJWTString = try String(data: headerJWTData,encoding:String.Encoding.utf8) as! String 
} catch { 
    print("string could not be created") 
} 

//Convert headerJWTString to base64EncodedString 
do { 
    let headerJWTBase64 = try Data(headerJWTString.utf8).base64EncodedString() 
} catch { 
"base64 could not be created" 
} 

Répondre

0

Une fois que vous créez le Data d'utiliser JSONSerialization, vous utilisez simplement la méthode de Data pour obtenir une chaîne codée base64.

let headerJWT: [Dictionary] = ["alg":"RS256","typ":"JWT"] 

do { 
    let headerJWTData: Data = try? JSONSerialization.data(withJSONObject:headerJWT,options: JSONSerialization.WritingOptions.prettyPrinted) 
    let headerJWTBase64 = headerJWTData.base64EncodedString() 
} catch headerError.customerError { 
    print("could not make data") 
} 

Vous pouvez passer différentes options pour base64EncodedString() en fonction de ce format que vous avez besoin de la chaîne base64 pour être.