Difference between NSString *const and const NSString *? [duplicate] - objective-c

This question already has answers here:
Objective-C const NSString * vs NSString * const
(2 answers)
Closed 8 years ago.
Can someone tell me the difference between the following 2 declarations of NSString?
NSString *const userName = #"ABC";
and
const NSString *userName = #"ABC";

The first is a constant pointer to an NSString object, while the second is a pointer to a constant NSString object.
First - A constant pointer (not modifiable) to an NSString (its value can be modified).
Second - A modifiable pointer to a constant NSString (its value can't be modified).

Related

How to fix duplicate symbol error in Objective-C? [duplicate]

This question already has answers here:
Duplicate Symbol Error in Objective-C build?
(17 answers)
Closed 7 years ago.
the variable is inside Constants.h
ERROR is:
duplicate symbol _OSVShortTermFuelTrim1 in:
/Users/loximity/Library/Developer/Xcode/DerivedData/AutoCodesApp-hjoxbttreaujifdypikhbngdqihd/Build/Intermediates/AutoCodesApp.build/Debug-iphonesimulator/AutoCodesApp.build/Objects-normal/i386/ViewController.o
/Users/loximity/Library/Developer/Xcode/DerivedData/AutoCodesApp-hjoxbttreaujifdypikhbngdqihd/Build/Intermediates/AutoCodesApp.build/Debug-iphonesimulator/AutoCodesApp.build/Objects-normal/i386/FuelTrimViewController.o
and then I am using the above variables in two places, in view controller:
OSVShortTermFuelTrim1 = [NSString stringWithFormat:#"%# %#", [sensor valueStringForMeasurement1:NO], [sensor imperialUnitString]];
and in FuelTrimViewController"
fuelBank1.text = [NSString stringWithFormat:#"%#",OSVShortTermFuelTrim1];
You have to use extern to the declare the constants in the header file:
extern NSString *const YOUR_CONSTANT;
and then in an implementation file (.m), define the value:
NSString *const YOUR_CONSTANT = #"Hello World";
and, incidentally, your example code is the same, for all intents and purposes, to:
fuelBank1.text = OSVShortTermFuelTrim1;

Convert NSUInteger to string with ARC [duplicate]

This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

How do you assign characterAtIndex to NSString variable? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've looked at several posts to answer my question but i keep getting errors.
I basically have an NSString that holds 2 characters...
myString = #"ABC";
All I want to do is assign the first character in myString to another NSString variable...
NSString *firstChar;
firstChar = [myString characterAtIndex:0];
I get this error when i do this:
Incompatible integer to pointer conversion assigning to 'unichar *' (aka 'unsigned short *') from 'unichar' (aka 'unsigned short')
What am I doing wrong?
NSString's characterAtIndex: returns a unichar, not a string, so you can't just assign it to another string. You need to create a string from it first:
NSString *myString = #"ABC";
unichar firstChar = [myString characterAtIndex:0];
NSString *string = [NSString stringWithCharacters:&firstChar length:1];
Of course, as DarkDust says, you can also use substringWithRange: to get a string from a subrange of a string:
[myString subStringWithRange:NSMakeRange(0, 1)]; // to get the first character
[myString subStringWithRange:NSMakeRange(1, 2)]; // to get the second and third characters
The characterAtIndex: method returns a unichar (an integer representation of the character). What you want is -[NSString substringWithRange:], like [myString substringWithRange:NSMakeRange(0, 1)];

how to get extension file NSString [duplicate]

This question already has answers here:
Get the extension of a file contained in an NSString
(6 answers)
Closed 9 years ago.
I want get extension (format file from NSString) but I dont know about it please tell me syntax for getting format file from NSString.
NSString *name = #"Xcode-tutorial.pdf"
I want get .pdf format and store in one variable
Use NSStrings pathExtension method:
NSString *fileExtension = [name pathExtension];
Did you look in the documentation? There is a method that does just that:
NSString *ext = [name pathExtension];
NSLog(#"%#", ext);
use this my dear:
NSString *a = [name pathExtension];

How to get the substring of a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get substring of nsstring?
how do I get a substring in iOS?
I am new to iphone.In my project i have a string such as chapter1 but actually i want the substring 1 only how it is possible.if any body know this please help me
Check out the substringFromIndex: or substringToIndex: methods of NSString. You can find the documentation for NSString here.
Ex:
NSString *string = #"Chapter1";
NSString *newString = [string subStringFromIndex: ([string length] - 1)]; // => "1"