iphone program warnings - iphone

I have some code added in viewWillAppear;
curr_rep_date = [tmpRptDt
stringByReplacingOccurrencesOfString:[NSString
stringWithFormat:#"%d",tmpYrVal]
withString:[NSString
stringWithFormat:#"%d",(tmpCurrYearInt-2)]];
When I build, I get the following warning;
warning: incompatible Objective-C types assigning 'struct NSArray *', expected 'struct NSMutableArray * }
Also
warning: assignment makes pointer from integer without a cast for:
replist_rptdt_dict =
PerformXMLXPathQuery(xmlData,
#"//XX/Period[#XX]");
Please let me know the reason.
Thanks.

replist_rptdt_dict = PerformXMLXPathQuery(xmlData, #"//XX/Period[#XX]");
First, the Objective-C standard is to use camel cased english names for variables. replist_rptdt_dict is confusing (it almost sounds like you have a list dictionary something what huh?).
warning: incompatible Objective-C
types assigning 'struct NSArray *',
expected 'struct NSMutableArray *' }
This will happen if you have:
- (NSArray *) foo;
...
{
NSMutableArray *bar = [someObject foo];
}
That is, bar is a more specific type -- a subclass -- than foo's return value. The compiler is complaining because your code is quite likely going to crash if you send, say, removeObjectAtIndex: to what is quite likely an immutable array.

Related

Return type from valueForKeyPath:?

This is probably pilot error on my part, but I am a little confused why this does not return an int (as thats the type of the property identified by the key path). Does valueForKeyPath: return an object instead, can anyone explain.
// Simple Object
#interface Hopper : NSObject
#property(nonatomic, assign) int mass;
#end
// Test
Hopper *hopper = [[Hopper alloc] init];
[hopper setMass:67];
NSLog(#"HOPPER: %d", [hopper valueForKeyPath:#"mass"]);
.
WARNING: Conversion specifies type 'int' but the argument has type 'id'
Yes, it returns an objc object:
- (id)valueForKeyPath:(NSString *)keyPath;
Details for automatic conversions from non-objc objects to objc objects (e.g. NSNumber and NSValue) is covered in Accessor Search Patterns for Simple Attributes.
Therefore, you would use the objc object format specifier %#:
NSLog(#"HOPPER: %#", [hopper valueForKeyPath:#"mass"]);
valueForKeyPath returns an object. int and char types are not objects. Access the property via the . operator or similar.
NSLog(#"HOPPER: %d", [hopper mass]);
NSLog(#"HOPPER: %d", hopper.mass);
Edit: Didn't fully read example code, updated answer

XCODE incompatible pointer type error

I am getting a casting error. My app is reading a text file from a webpage using 'stringWithContentsOfURL' method. I want to parse the individual lines into separate components. This is a snippet of the code.
int parameterFive_1 = 0;
parameterFive_1_range = NSMakeRange(0,10)
lines = [response componentsSeparatedByString:#"\r"];
parameterFive_1 = CFStringGetIntValue([[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
I am getting the following error message:
" Implicit conversion of an Objective-C pointer to 'CFStringRef' (aka 'const struct __CFString *') is disallowed with ARC"
I thought it might be the compiler option but changing it to the default is not making a difference. Can anyone provide any insight?
Just cast the NSString* to CFStringRef to satisfy ARC:
parameterFive_1 = CFStringGetIntValue((__bridge CFStringRef)[[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
The __bridge keyword here lets ARC know that it doesn't need to transfer ownership of the string.

I get error: incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray?

NSMutableArray *sortedReleases = [theReleases sortedArrayUsingComparator:^(id a, id b)
I get the following:
Incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'
I have to have sortedReleases as a NSMutableArray. How do I get past it?
Thanks
This should do the trick I think (written in browser) — NSMutableArray inherits from NSArray, so you can use the arrayWithArray constructor.
NSMutableArray *sortedReleases = [NSMutableArray arrayWithArray:[theReleases sortedArrayUsingComparator:^(id a, id b)]]
sortedArrayUsingComparator returns a NSArray*, you want a NSMutableArray*. In ObjC the mutable types inherit from the immutable types, so you can't just use the NSArray*. You need something to take your NSArray* and give you the equivolent NSMutableArray*. In this case the NSMutableArray arrayWithArray method will do it.
Try something like:
NSMutableArray *immutableSortedReleases = [theReleases sortedArrayUsingComparator:^(id a, id b)...]
NSMutableArray *sortedReleases = [NSMutableArray arrayWithArray:immutableSortedReleases];
Or if sufficiently clear, skip having immutableSortedReleases, and put the arrayWithArray call inline.

warning when using BOOL variable in objective-c

I am trying to initalize my BOOL variable to YES but its giving me this warning.. not quite sure what to do.. it still seems to be working fine but just wondering how I can get rid of the warning.
I have initalize the variable in the header like this
//.h
BOOL *removeActivityIndicator;
//..
#property (nonatomic, assign) BOOL *removeActivityIndicator;
Then I try to set it to YES like so (this is also where I get the warning)
self.removeActivityIndicator = YES;
The warning says :
incompatible integer to pointer conversion passing 'BOOL' (aka
'signed char') to paramater of type 'BOOL *' (aka 'signed char *')
The warning is correct; you've declared the variable as a BOOL * (a pointer to a BOOL), which is almost certainly not what you want. Remove the * from the declaration.
removeActivityIndicator is a char pointer, and you assigns a char to it, so either:
change it to be BOOL removeActivityIndicator;
Dereference it: *(self.removeActivityIndicator) = YES;
You've made a pointer to a BOOL, which is a primitive type. Remove the extra * in front of remoteActivityIndicator.

Obj-C: Difference between "Fairfield" and #"Fairfield" (with at string)?

I just had a ridonkulous typo in my iPhone app, answered here.
Now I'm wondering about the #"..." notation.
why this works:
NSArray *someArray = [NSArray arrayWithObjects: #"Fairfield", nil];
and this does not (even though it compiles, it will throw an EXC_BAD_ACCESS):
NSArray *someArray = [NSArray arrayWithObjects: "#Fairfield", nil];
Edit:
Ok, so you guys have pointed out that I can't add a C string to an NSArray, because it's obviously not an object.
Now another question: Isn't this somewhat of an oversight? I mean, why does the "...WithObjects:" message specify a list of (id) instead of (NSObject *)?
"#Fairfield" is a normal C string with an '#' character in it. #"Fairfield" is an Objective-C string (NSString on OS X) with no literal '#' in it.
You cannot add C strings to Cocoa collections.
It accepts id rather than NSObject because all initialisers return id. All initialisers return id because subclasses would otherwise override the return type of their ancestors' initialisers.
For example, -[NSMutableString init] can't return NSMutableString * because it subclasses -[NSString init], which can't return NSString * because it overrides -[NSObject init].
Unfortunately, implicit type-casting between const char * and id is perfectly legit, so the compiler won't throw a warning, however a static analyser may be able to pick this sort of mishap up fairly easily.
"Fairfield" is a C string, #"Fairfield" is an Objective-C string.
#"Fairfield" is an object (NSString), so you can send it methods ([#"Fairfield" uppercaseString]) and add it to Objective-C arrays ([NSArray arrayWithObjects:#"Fairfield",nil]). You can only add objects to NSArrays.
On the other hand, "Fairfield" is a C string, and is generally not used in Cocoa. For the most part, you can get by with only using #"Fairfield"
The other reason that a number of things in Cocoa deal with id rather than NSObject* is because, unlike some other languages (say, Java and C#), where all objects in the language must inherit from some global base class, it's entirely possible to have objects that do not descend from NSObject (NSProxy being one example). It's not something you'd do often, but it is possible. The id type means "pointer to any Objective C instance".