Matoe
Matoe

Reputation: 2758

Is there a method for adding one NSString for NSData?

I'm working on writing to a file one user input on a textField. So far I have one NSFileManager which writes data to a file. Still, I have no way of putting the textField input inside a file.

Is there a way do add a string value to NSData so I can write it?

Upvotes: 1

Views: 3807

Answers (2)

Tatvamasi
Tatvamasi

Reputation: 2547

you can get NSData from NSString,

NSData *newData = [yourString dataUsingEncoding:NSUTF16StringEncoding];

use encoding that fits your case. append the obtained data to existing NSData,

[existingData appendData:newData]

Note: "existingData" should be an instance of NSMutableData.

Upvotes: 4

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use below code as your reference.

NSString* myString   = @"Is there a method for adding one NSString for NSData";
NSData* myData=   [myString dataUsingEncoding:NSUTF8StringEncoding];

Upvotes: 0

Related Questions