Dynamical value in define var_name block - ios

In my app i have such thing
#define ACCT_ID #"someaddress#gmail.com"
Is there any way to set dynamical value to this define block?
FOr ex. i make request to webserver, which returns me some string, and later i set this string into #define block.
Is there anyway to achieve this?

you also use nsuserdefault like:
set value
[[NSUserDefaults standardUserDefaults]setObject:#"abc#gmail.com" forKey:#"ACCT_ID"];
get value
NSString *str= [[NSUserDefaults standardUserDefaults]valueForKey:#"ACCT_ID"];
remove value
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"ACCT_ID"]

Well of course not, since all lines begin with # happening during pre-processor compilation.
Therefor, the value given to ACCT_ID detemind before even your program compiled and it can not be changed

Ended up with following:
#define ACCT_ID [[SingletonClass myinstance] getEmail]

Related

Macros Retains Static Values

Below is a Macro which i use to select the IP. An alertview is popped up user tries to login based on that the respective IP is set to fetch the data from the Server.
static NSString *updateProfileDetails_URL=#"http://%#/api/Home/editProfile/ios/1";
#define getServerURl(url,selectdServer)[[NSString stringWithFormat:#"%#",url] stringByReplacingOccurrencesOfString:#"%#"
withString:([selectdServer isEqualToString:#"live"] ?#"live_ip/folder_name":#"demo_ip/folder_name" )]
I selected the 'demo_ip' option login.
Logged out.
Selected 'live' option now.
Problem here is that the demo_ip is called at certain places. Kindly help.
Thanks
You can try like this:
#define USE_TEST_URL 1 // use 1 for test and 0 for live
#if USE_TEST_URL // define test urls here
#define API_URL #"http://...<TEST URL>"
#else // define live urls here
#define API_URL #"http://... <LIVE URL>"
#endif
and
NSString *url =[[NSString stringWithFormat:#"%#",API_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
There is none of the issue with Macro, There might've been a problem with your passing parameters, when you calling getServerURl(...)
When you need live, Make sure you passing 'live' in second parameter of getServerURl(...)! Because you are conditionally comparing 'live' lowercase value.
For more information: Macros are replaced by the preprocessor by their value before your source file even compiles. So There is no way you'd be able to change the value of the macro at runtime.

Value stored to NSString during its initialization is never read

In my iOS app I have following code:
case SASpeechSubCase03:
{
SAActivity currentActivity = self.mediator.selectedActivity;
NSString *sActivity = NSLocalizedString(#"activity", #"activity");
NSString *sActivity2 = NSLocalizedString(#"another activity", #"another activity");
if(currentActivity == SAActivityWalk)
{
sActivity = NSLocalizedString(#"walk", #"walk");
sActivity2 = NSLocalizedString(#"walking", #"walking");
}
else
{
sActivity = NSLocalizedString(#"run", #"run");
sActivity2 = NSLocalizedString(#"jogging", #"jogging");
}
return [NSString stringWithFormat:speech.text, sActivity, sActivity2];
break;
}
When I run bots on it, it gave me following warning:
Bot Issue: analyzerWarning. Dead store.
Issue: Value stored to 'sActivity' during its initialization is never read.
File: SAAnnouncementService.m.
Integration Number: 42.
Description: Value stored to 'sActivity' during its initialization is never read.
Bot Issue: analyzerWarning. Dead store.
Issue: Value stored to 'sActivity2' during its initialization is never read.
File: SAAnnouncementService.m.
Integration Number: 42.
Description: Value stored to 'sActivity2' during its initialization is never read.
Can someone tell what the problem might be here?
Any kind of help is highly appreciated!
The problem is that you initialized the variables and then directly started the if-else blocks, without using, i.e. reading, the initial values.
When execution gets to the if-else blocks, it will definitely be assigned a new value, no matter what value it was before.
With the following line :
NSString *sActivity = NSLocalizedString(#"activity", #"activity");
NSString *sActivity2 = NSLocalizedString(#"another activity", #"another activity");
You are assigning string values to the sActivity and sActivity2 objects.
Then, these two values are modified in either if or else statement.
But, as the static analyzer mentions, the initial values of these objects (#"activity" and #"another activity") were never read before the second assignment (in if / else statement).
To avoid this warning you can replace the two lines above, by :
NSString *sActivity = nil;
NSString *sActivity2 = nil;
Hope that helps ;)
When you get a warning, the compiler tells you "what you are doing here looks like nonsense, and is most likely not what you want".
Look at these two statements:
NSString *sActivity = NSLocalizedString(#"activity", #"activity");
NSString *sActivity2 = NSLocalizedString(#"another activity", #"another activity");
Does the assignment serve any purpose? It doesn't look like it. So the compiler thinks "either the guy made a rather expensive call that is completely pointless, or he actually intended to use the result of NSLocalizedString but stored it in the wrong place. "
Since the compiler assumes that people don't do pointless things, it assumes that there is a bug in your code and tells you about it. It's the kind of thing where a human reviewing your code would stop and ask you what you were intending to do there.
In your codes, sActivity would be set to either walk or run within IF/ELSE, so that the value set for sActivity this line
NSString *sActivity = NSLocalizedString(#"activity", #"activity");
would never be read. It might not cause error but analyzer reminded you about this superfluous initialization. Try NSString *sActivity=nil;, see if the warning could be turned down.
You are not using sActivity in if-else blocks, you are simply assigning it values based on decision, So either take it nil string like
sActivity = nil;
or like
NSString *sActivity;
to remove waring .

UITextfield to NSUserDefaults type error

I have a UITextField and I wish to extract the integer value from it and put it into a NSUserdefaults.
#property (weak, nonatomic) IBOutlet UITextField *defaultTipPercentage;
[defaults setInteger: defaultTipPercentage forKey:#"another_key_that_you_choose"];
I'm getting an error saying
Use of undeclared identifer 'defaultTipPercentage'. did you mean '_defaultTipPercentage?'
I tried this but I get an error
Incompatible pointer to integer conversion sending 'UITextField *_weak' to parameter of type 'NSInteger'(aka 'Int')
Pretty much just incompatible types. I was wondering what would be a quick fix for this
You have 2 problems.
You can't write a text field directly to user defaults - you need the text, or rather, the integer value of the text.
With a property, you either need to refer to it using self.defaultTipPercentage (which uses the getter), or _defaultTipPercentage, which refers to the backing instance variable directly. Using self.xxx syntax is generally safer.
Jenox already posted code to solve the first problem in the comments.
To also fix the second problem, you need to use property list syntax:
NSInteger tip = [self.defaultTipPercentage.text integerValue];
[defaults setInteger:tip forKey:#"another_key_that_you_choose"];
NSInteger tip = [self.defaultTipPercentage.text integerValue];
[defaults setInteger:tip forKey:#"another_key_that_you_choose"];

Remove NSLog header

Below is a typical NSLog output from the console. Can I get rid of the bold text?
2013-06-09 22:17:02.351 ProjectName[33584:907] MyWantedText
I want to cut out the console text, and compare it (by diff), to a similar log. I don't want time data etc that only will produce false positives.
Is it possible to make my own console write method, MyNsLog, if I can't alter de behavior NSLog?
for your app put this into your Prefix header:
#undef NSLog
#define NSLog(fmt, ...) printf("%s", [[NSString stringWithFormat:fmt, ##__VA_ARGS__] UTF8String])
but id actually rather leave nslog and just use another logging mechanism like ddlog or so
Simple,
NSString *text = #"Some text here";
printf("%s", [text UTF8String]);
Result,
Some text here
You can't rid of the bold text, NSLog() adds the capability to print out objects variables. Also as you can see, adds the program name, the date, the time.
You can change for another logging function like printf

UIProgressView value from plist inside UITableViewCell

I'm trying to pass a number from a plist to a UIProgressView inside a UITableViewCell. So far, I have done this:
myUIProgressView.progress =[[myArray objectAtIndex:indexValue] valueForKey:#"myUIProgressViewValue"];
However, when doing this I get an error: Assigning to 'float' from incompatible type 'id'
How can I fix this?
Also, the value coming from the plist is not a decimal (because the number is used more than once), but instead a whole number. I need to somehow add a . before the number within that piece of code above, but I am not sure how to go about that.
Try this:
myUIProgressView.progress = [[[myArray objectAtIndex:indexValue] valueForKey:#"myUIProgressViewValue"] floatValue] / 10;
Try This
myUIProgressView.progress =[[[myArray objectAtIndex:indexValue] floatValue]valueForKey:#"myUIProgressViewValue"];

Resources