Value stored in 'parameter name' is never read in commented Macro - ios

I'm using different Macros in order to log different sections of my app. This way I can "turn off" some of the logging using comments.
Example:
String *logValue = #"This is some log text")
and then:
NetworkLog(#"%#", logValue);
SessionLog(#"%#", logValue);
I can turn off all session logs in my app by commenting that specific Macro code and leaving the network logs active.
When I turn off that specific Macro I get an analyzer warning:
Value stored in 'logValue' is never read
How can I solve this issue?

If you mark the variable as "unused", that should prevent the warning.
__unused NSString *logValue = #"This is some log text";
or
__attribute__((unused)) NSString *logValue = #"This is some log text";

Related

Unity / Objective-C: Trouble converting char* to NSString

I have what should be a dead-simple piece of code that is failing for me in strange ways.
void MediaShare(char* text, char* furl) {
NSString *status = [NSString stringWithUTF8String: text];
NSString *media = [NSString stringWithUTF8String: furl];
[[SocialShare sharedInstance] mediaShare:status media:media];
text is just a line of text for Twitter sharing, and furl is just a file location string. I am getting a crash down past this function that comes down to bad data getting passed. Putting a breakpoint at the head of this function yields the following-
Image of Xcode variable monitor
The two values look fine, although not sure if the * values that only contain the first char are a problem.
Anyway, jumping to the end, and status and media appear to be converted to hex values.
Converted to hex values?
Any ideas? To give the full story, this is a Unity plug-in. I am simply passing in two strings from Unity C# to this function.
Thanks so much.
The code looks fine so far, if the input values are well formed C char* strings that can be interpreted as an UTF-8 encoded string.
status and media being hex values at the end of the function hint that they are. Both are pointers to Objective-C objects, so this is expected. Print them to the debug console or use po status at the debug console to check their contents (it will print the result of the -description method of status which is the string content in that case).
The subsequent crash might be caused elsewhere in the code.
What's the crash's log output?

PKPayment doesn't always return a name

When creating my PKPaymentRequest I specify some required fields like this:
request.requiredBillingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldEmail|PKAddressFieldName;
request.requiredShippingAddressFields = PKAddressFieldEmail|PKAddressFieldName;
Note: I only need billing email and name but if I don't specify the PKAddressFieldEmail|PKAddressFieldName on requiredShippingAddressFields as well the ApplePay UI does not ask for this.
Then I try to extract the name like this. It is not always clear if it arrives in the shipping address or the billing address so I check for both.
NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(payment.shippingAddress);
if (!name) {
name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(payment.billingAddress);
}
But sometimes the name just doesn't show up. It's intermittent, sometimes it does show up, sometimes it does not without changing anything. This makes me suspect it's a bug in iOS 8.4 but I'm definitely not ruling out it's my fault. (It usually is.)
Has anyone else run into this? Is there a workaround?

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 .

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

How to specify localized property list?

I have an ios application which is calling a rest web service. I have created a property list file in my app which contains a dictionary of error codes that will be returned by the server and a corresponding message to show.
I am thinking of having multiple property list files for different languages.
How can i make the app to pick up a specific property list based on locale?
You say that the server is generating an error code and a message.
So I think there are more ways to do it.
First you could send a locale header in your request to the server, so that the server would do all localizations for you (which in my opinion is not as good as solution #2).
So I would prefer the way of letting the server just return error codes and handling the messages on client side.
You could create a language project in xcode, for each language you want to support:
http://www.ibabbleon.com/iphone_app_localization.html#extract
In the localizable.strings file I would do the following:
"RestServiceXYErrorTitle_1" = "Authentication failed";
"RestServiceXYErrorMessage_1" = "Your credentials were wrong";
"RestServiceXYErrorTitle_2" = "Resource not available";
"RestServiceXYErrorMessage_2" = "The resource you requested is no longer available";
....
Then I would take the error code returned by the server, e.g. 1 and put it together with my localization string:
NSString *localizedTitleKey = [NSString stringWithFormat:#"RestServiceXYErrorTitle_%#", errorCode];
NSString *localizedMessageKey = [NSString stringWithFormat:#"RestServiceXYErrorMessage_%#", errorCode];
NSString *errorTitle = NSLocalizedString(localizedTitleKey,#"");
NSString *errorMessage = NSLocalizedString(localizedMessageKey,#"");
I think this would be a good solution

Resources