Incompatible Objective C types - iphone

I am trying to store the location of my Application's Document Directory in a NSString.
NSString *documentDirectory = [self applicationDocumentsDirectory];
The code compiles without error, but I get the warning that I am trying to initialize a NSString with a NSURL. When I run the app, the app crashes. I tried casting, but it gave me an error. The book I got it off(Head First iPhone Development) says the code should work. The code is in my appDelegate.m file. Any Help will be greatly appreciated.
Regards

It is returning an NSURL as Dave DeLong said. However the easiest way to get round this and convert it to an NSString without changing the method, is setting the string to the NSURL's absoluteString.
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];

Your -applicationDocumentsDirectory method is returning an NSURL. You want it to return an NSString.

The methods return a NSURL. You must convert the url in NSString.
It's very easy. Use a method in NSURL class.

Related

Declare as NSString but Xcode treat as NSdata & received incompatible passing

I'm trying to pass an NSString address object to a UILabel text, but I received a warning from XCode compiler about incompatible pointer, as shown in the screen shot below:
And from the screen shot below you can see that my address object is declared as a NSString.
I had try displaying my NSString with:
NSLog(#"%#",[[LocationData objectAtIndex:rowDisplay] address]);
and it works without any incompatible pointer error. Can anyone please help? I have done some researching but I still can't find any solution.
My address is an object which gets stored into an NSArray. The address format is the always the same, for example "542 W 112 St New York NY 10025".
Thanks.
This might be due to the fact that there already is a method/property called address in some of SDK classes, and it happens to return NSData *.
NSArray's objectAtIndex: returns an object of type id, and the compiler doesn't know that it's your custom class that has address defined to return NSString *. When it tries to match address selector, it takes the one from SDK, and not yours.
You can however cast the returned object to your class and have your method address called:
[(YourClass *)[LocationData objectAtIndex:rowDisplay] address];
You don't see the warning when outputting address to NSLog since %# format accepts both NSString * and NSData * (for classes other than NSString it actually outputs the result of description method, which returns NSString *).
there is many follow up questions with your question. However, you said, you can show with :
NSLog(#"%#",[[LocationData objectAtIndex:rowDisplay] address]);
so why not use this into UILabel :
self.displayAddress.text = [NSString stringWithFormat:#"%#"[[LocationData objectAtIndex:rowDisplay] address]];
PS: please don't use image for 1 line of code. i have to retype your code here...

How to convert NSString to/from TCHAR (using CLucene)

I'm developing an application for iPhone, in which I've imported c++ CLucene library.
Almost all CLucene functiona require String as TCHAR*.
I've some problems in converting NSString to/from this type of data. I've searched many solution but none worked.
Can you please show me how to make this conversion?
I was able to make library work with test string defined with _T() macro. However XCode give me:
Conversion from string literal to 'TCHAR *' (aka 'wchar_t *') is deprecated.
Which is the non deprecated method to do it?
Thank you!
Edit:
I solved this way:
Converting from NSString to TCHAR*:
(const TCHAR *) [stringa cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
Converting from TCHAR* to NSString:
[[NSString alloc] initWithBytes:ctc length:wcslen(ctc) * sizeof(TCHAR) encoding:NSUTF32LittleEndianStringEncoding]
thank you!
Following code worked for me:
NSString *pStr = [NSString stringWithFormat:#"%S", GetTCHARString()];

Losing NSString

I'm copying an NSString from an NSDictionary into a local NSString using [[NSString alloc] initWithString:], processing it (removing some chars) then sending it to an external object.
I've used two methods a [[MyObject alloc] initWithString:] and [myObject setString:]; neither work.
Both of them make my app crash; when I use a breakpoint on the given area I get an empty NSString; I use NSLogs from the start of the NSString until I send it to my object; they all show the string's correct value…
Thank you all for your valuable input :-)
Replace the [[NSString alloc] initWithString:stringBeingCopied] call with [[NSMutableString alloc] initWithString:stringBeingCopied], creating a mutable string, as opposed to an immutable ("normal") string will stop the setString: calls from crashing your app
Immutable strings, being immutable, don't respond to setString: calls - which are NSString mutators. Creating an NSMutableString instead, which implements setString:, will let you modify the string object.
I've used two methods a [[MyObject
alloc] initWithString:] and [myObject
setString:]; neither work.
Show your work! Can't help you without showing more code. In particular, how did you implement initWithString: and setString: on your MyObject class?
Strings are normally immutable. To modify a string, you need a NSMutableString. Either do [string mutableCopy], or do this from inside the initWithString method. Maybe you do this already, but please post your code if possible. I'm afraid it's hard to give any further help without it...

How do I write ALAsset in a NSData

I'm trying to write ALAsset in a NSData and somehow came up with the following code which does not work. What I would like to know is, whether the code is similar to this? Or is there another way of doing it?
NSData *data = [[NSData alloc] initWithContentsOfFile:#"assets-library://asset/asset.MOV? id=100000009&ext=MOV" ];
The defaultRepresentation of an ALAsset has a getBytes:fromOffset:length:error: method. Use that.
There's a space between your "?" and "id=.." is this the problem?
The best way to debug this is to first try opening that file you name (or creating an NSURL and using initWithContentsOfURL as suggested by Max) and then running the debugger, checking whether that succeeds. Right now you're debugging a few things simultaneously, break out that compound statement and figure out what could be going wrong

iPhone: Missing method? NSDictionary dictionaryWithData:

A web service provides data in the form of a plist. After the download, I have all of the data in an NSData object, which I want to convert to an NSDictionary. Right now the only way I know of to do that without parsing it by hand (yuck) is this:
static NSString *fileName = #"tempFile";
[data writeToFile: fileName atomically: NO];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fileName];
I'm surprised NSDictionary doesn't have a dictionaryWithData: method. I wouldn't even mind converting the NSData to an NSString first, if dictionaryWithString: existed. Clearly the SDK is doing something similar under the hood, but it isn't exposed to developers.
Can anyone suggest a better way to do this? One of the files I need to convert can potentially be pretty big, and doing the write-to-file+read-from-file operations could be a bit slow.
Use one of the following:
+[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
+[NSPropertyListSerialization propertyListWithData:options:format:error:]
The former is available in iOS 2.0+ but has been deprecated in 4.0.