how to get extension file NSString [duplicate] - objective-c

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

Related

How to print text from array with some other string? [duplicate]

This question already has answers here:
Append string with variable
(4 answers)
Shortcuts in Objective-C to concatenate NSStrings
(30 answers)
Closed 5 years ago.
I'm new to Objective-C and I couldn't find a direct solution for my problem where it is explained how to add string behind or in front of an array value .
I try to get some text from an array and add some other string behind or infront of it (will be used in tableviewcell). In Android it's working just with + signs, that's why I couldn't handle it here.
I try to do something like below:
"some text" +[arrayName objectAtIndex:indexPath.row] + "some text"
You need to use stringWithFormat to concatenate those values, or you can use NSMutableString as well
Use this
NSString* finalText = [NSString stringWithFormat:#"some text %# some text",[arrayName objectAtIndex:indexPath.row]];
Another way
NSMutableString* finalString = [NSMutableString stringWithString:#"test Text"];
[finalString appendString:[arrayName objectAtIndex:indexPath.row]];
[finalString appendString:#"test Text 2"];
Short:
[NSString stringWithFormat:#"%#/%#/%#", "some text", [arrayName objectAtIndex:indexPath.row], "some text"];
For further answers see: https://stackoverflow.com/a/510444/9310455

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

How can I use a String in more than one file? [duplicate]

This question already has answers here:
Constants in Objective-C
(14 answers)
Closed 8 years ago.
in my view controller.m i have a string like this
NSString *valueToSave = #"someValue";
and would like to safe the text with NSUserDefauls in Appdelegate.m
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:#"preferenceName"];
how can i use the NSString in the other file?
This doesn't work:
#import "viewcontroller.m";
In a header file have extern NSString *valueToSave;. Then in a (1 and only 1) .m file have NSString *valueToSave = #"someValue";
A second option would be to use a #define. Simply put #define kValueToSave (#"someValue") in a header file and use it where you need it.