Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert String to NSData and NSData to String Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.54k
    Comment on it

    Hi All,

    In coding we have secure some information while we are sending it to server and vice versa ,In this case we use encoding and decoding . You can encode your information by using any method of encoding and when you will receive information from server then you have to decode that information using same criteria which is used by server side code to encode that information. For example

    If you want to convert String to NSData  using NSUTF8StringEncoding :-

    var stri:String = "this is my string to convert to NSData"
    
    let utf8str = stri.dataUsingEncoding(NSUTF8StringEncoding)

    In above code, we have encoded stri to DATA using UTF8 encoding. When you have some data which is encoded in UTF8 Data than you can use below code

    NSDATA is String using NSUTF8StringEncoding :-

    let attributedOptions : [String: AnyObject] = [
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
    ]
    let attributedString = try NSAttributedString.init(data: utf8str!, options: attributedOptions, documentAttributes: nil)
    
    print(attributedString.string)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: