-2

I am trying to write a NSData variable to a file as a string using the following code:

 [filehandle writeData:[myData dataUsingEncoding:NSUTF8StringEncoding]];

And I am getting the error: No visible @interface for 'NSData' declares the selector 'dataUsingEncoding:'

user391686
  • 97
  • 2
  • 12
  • Have a look at this http://stackoverflow.com/questions/6428461/convert-nsdata-to-string – Nauman Apr 25 '13 at 20:43
  • I am sorry I used a terrible code example. I am actually trying to wrtite it to a text file as a string. – user391686 Apr 25 '13 at 20:44
  • Hint: The first noun of an Objective-C function name is usually the type of object returned by the function. – Hot Licks Apr 25 '13 at 20:45
  • https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html – Hot Licks Apr 25 '13 at 20:47
  • And https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html – Hot Licks Apr 25 '13 at 20:49
  • Is myData of type `NSString` or `NSData` cause only NSString has dataUsingEncoding: and not NSData – Pfitz Apr 25 '13 at 20:49
  • 1
    (The message is telling you that there is NO SUCH METHOD for NSData.) – Hot Licks Apr 25 '13 at 20:50
  • Then why is xcode suggesting that I use it? All of the code examples I've seen online use it. – user391686 Apr 25 '13 at 20:51
  • I had a string. I had to convert it into NSData to use the filehandler to write it to the file. While writing it to the file I am trying to write it back as a string. – user391686 Apr 25 '13 at 20:52
  • Just how is Xcode "suggesting" that? And where is a code example online that uses it?? – Hot Licks Apr 26 '13 at 18:48

2 Answers2

2

dataUsingEncoding: isn't an NSData method. I think you want to turn the NSData into an NSString and then ask the string for its dataUsingEncoding:.

EDIT: You indicate in your comments that this data was derived from an existing NSString. In that case, just get the data with [yourString dataUsingEncoding:NSUTF8StringEncoding] and then you can just [filehandle writeData:myData].

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

You are trying to call a method which is implemented by NSString on an NSData object which does not know the dataUsingEncoding: method.

Pfitz
  • 7,336
  • 4
  • 38
  • 51