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

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;

Related

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

Difference between NSString *const and const NSString *? [duplicate]

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

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

Concatenate constants Objective-C ios [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create constant NSString by concatenating strings in Obj-C?
I have two constants that I would like to concatenate:
NSString * const WEBSITE_URL = #"http://192.168.1.15:3000/";
NSString * const API_URL = #"http://192.168.1.15:3000/api/";
Normally in other languages I would concatenate the WEBSITE_URL in API_URL, but you can't concatenate a compile time constant since stringWithFormat or anything like it is a runtime, not compile time method.
you can do this with macro
use:
#define WEBSITE_URL #"http://192.168.1.15:3000/"
#define API_URL WEBSITE_URL #"api/"
You could drop to the preprocessor.
#define WEBSITE_URL_DEF "http://192.168.1.15:3000/"
NSString * const WEBSITE_URL = #WEBSITE_URL_DEF;
NSString * const API_URL = #WEBSITE_URL_DEF "api/";

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.