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

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()];

Related

Strange behavior of NSString

I have a NSString that will be passed as a parameter to my library's function, the NSString is passed by another library. The strange thing is, if I pass the NSString to the library call, the call will fail, but if I convert the NSString to int and then convert the int back to a NSString, every thing is fine.
But by printing it out using NSLog("%#"), the two strings are identical. What may cause this? Encoding?
When you
convert the NSString to int and then convert the int back to a NSString
you are basically creating a copy of the string, unrelated to the original one.
It would be interesting to know how you create the two strings to know what exactly happens. My guess is that the first one is deallocated too soon, so the call fails; the copy is retained and the call succeeds.
Just for a quick try:
when calling the function that is failing:
[self doMethodWithString:aString];
just do:
[aString retain];
[self doMethodWithString:aString];
If this works, then the memory management issue is confirmed, but for a real fix you should explain how you create the strings...

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...

Incompatible Objective C types

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.

Correctly debugging IPhone Apps on XCode

The line below has an error, can you see it with a naked eye?
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:"%# button pressed.", title];
When the Iphone simulator executes this line it crashes without any other information. I must be missing something about debugging. The console showed me nothing.
I was able to figure out the error, my question is not about that. My question is why I get a hard crash with no help from XCode. Without any clue it took me precious 5 minutes before I could realize my typo.
Any suggestions? Or when programming for IPhone I just need to be very careful with typos?
EDIT: I guess some people did not see it immediately like me. The error is the lack of '#' for the string constant. So now my question is why XCode/Simulator did not show me any kind of error message, just crashed without any clues. Am I missing something about debugging?
Objective-C does not strongly verify that the arguments you pass to messages are of the right type during compilation nor at runtime. It should gives you a warning though. Here you pass a c string instead of a NSString. Because NSString are objects (thus referenced using pointer), your method uses it as a pointer while you feed it with a simple string. You then probably try to access unaccessible memory blocks...
I think you miss a # before the "%# button pressed".
The correct one should be:
NSString *newText = [[NSString alloc] initWithFormat:#"%# button pressed.", title];
All the constant NSString should be #"SOMETHING HERE";
Check you compilation warnings. That's all you need. On the case you are showing, you will get a proper warning that will alert you that bad things might happen at that line.
I get the following Error when compiling your code:
error: cannot convert 'const char*' to 'NSString*' in argument passing
Not sure what you need to do to get it to show you that, I'm working in Obj-C++.
Try adding "-Wall" to your "OtherWarningFlags" under your target's build settings.

Has anyone had experience using the Objective-C bindings for YAJL?

http://github.com/gabriel/yajl-objc
I've already tried SBJSON, and while it works, I'm looking into alternative options to improve on parsing speed and memory consumption. Usage of this library doesn't seem to be as straightforward as SBJSON though, and I'm not sure how to begin using yajl.
Something like this:
NSArray *parsed = [data yajl_JSON];
Results in the following error:
-[NSConcreteMutableData yajl_JSON]: unrecognized selector sent to instance 0x5372180
Attempting to parse an NSString object results in the same problem. I can see the interface, but there doesn't seem to be an implementation... Am I not linking into the static lib properly?
Google turns up very little on usage examples. The doc on for the project itself only says the following about generating objects from json data/strings.
#import "NSObject+YAJL.h"
NSData *JSONData = [NSData dataWithContentsOfFile:#"example.json"];
NSArray *arrayFromData = [JSONData yajl_JSON];
NSString *JSONString = #"[\"Test\"]";
NSArray *arrayFromString = [JSONString yajl_JSON];
Which looks pretty much the same as what I've tried. What am I missing here?
You must have missed that part:
Under 'Other Linker Flags' in the Test target, add -ObjC and -all_load (So NSObject+YAJL category is loaded).
I had no problems with using YAJL, until I ran the app in Release mode, then I got the same error as you - turns out, I've only added these flags to Debug mode, not to all of them.
In general, I can recommend YAJL, it's definitely faster than all other alternatives (see benchmark results on my blog).