NSRangeException randomly crash app in object at index [closed] - crash

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
NSDictionary *latestCollection=[json objectWithString:responseString error:&error];
NSDictionary *data=[latestCollection valueForKey:#"results"];
if([data count]>0 && data!=nil)
{
arrayAddress=[[NSMutableArray alloc]initWithArray:[data valueForKey:#"formatted_address"]];
/*********Showing address of new location******/
[lblAddress setText:[NSString stringWithFormat:#"%#",[arrayAddress objectAtIndex:0]]];
[lblPickUpAddress setText:[NSString stringWithFormat:#"%#",[arrayAddress objectAtIndex:0]]];
}
else
{
NSLog(#"Address not available");
}

Your problem is your if clause:
if([data count]>0 && data!=nil)
if will evaluate from left to right, so first, it accesses data for the count, but it may be nil, so simply switch the expressions like so:
if( data!=nil && [data count]>0)
the if clause will stop, as soon as one of the expressions if false, so it will do a check for nil, if its NOT nil, then it will access data to get the count. If data is nil, then the data will not be accessed. Thats why your app crashes.

Related

Blocks in Swift shows error "Missing argument for parameter #2 in call" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm now using Jonas Gessner's JGActionSheet with Swift in my project, and the sample was written by Objective-C, when I tried to convert the block to Swift, Xcode shows the error "Missing argument for parameter #2 in call", here is the code I written and the screenshot:
Objective-C Sample
JGActionSheet *sheet = [JGActionSheet actionSheetWithSections:sections];
[sheet setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath)
{
[sheet dismissAnimated:YES];
}];
Code I written in Swift
let actionSheet = JGActionSheet(sections: sections)
actionSheet.buttonPressedBlock {
(sheet: JGActionSheet!, indexPath: NSIndexPath!) in
actionSheet.dismissAnimated(true)
}
Error screenshot
Missing argument for parameter #2 in call
So please help me to figure this out and thanks very much!
actionSheet.buttonPressedBlock is a property. You are trying to set it. So where's your equals sign? This is how you set things in Swift:
myThing.myProperty = myValue
The fact that you are trying to set this property to a block (a function) changes nothing. So:
let actionSheet = JGActionSheet(sections: sections)
actionSheet.buttonPressedBlock = {
(sheet: JGActionSheet!, indexPath: NSIndexPath!) in
actionSheet.dismissAnimated(true)
}

Check if object created: good style? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I recently did some functions like the following one:
-(BOOL)registerSomethingWithParameter:(Parameter*) param
{
Something* some = nil;
if ([self checkParameter:param])
{
some = [[Something alloc] myInitCallWithParameter:param];
}
return (some ? YES : NO);
}
There are many discussions about using the ? in code. What do you think? Is this a proper way to tell the calling function, that everything worked well without returning an object?
I also thought about: isn't it better to check for valid parameter in myInitCallWithParameter: within the Something-Definition, but mostly these Classes are very small and store only a few values. So everything that could result in creating a nil is checked when entering the if.
I don't think there's any problem in using ? instead of if/else. I see a lot of programmers using it and I use it myself. Your code style is fine.
Why not just do:
-(BOOL)registerSomethingWithParameter:(Parameter*) param
{
return [self checkParameter:param];
}
I'm assuming checkParamter returns a bool. In which case you don't even need this registerSomethingWithParameter function as it just creates a local variable that isn't used anywhere anyway ? Unless you've just written this as an example. :-)
While your approach is perfectly viable, a better option might be to simply return the created object itself; like so:
-(id)registerSomethingWithParameter:(Parameter*) param
{
Something* some = nil;
if ([self checkParameter:param]) {
some = [[Something alloc] myInitCallWithParameter:param];
[self registerSomething:some];
}
return some;
}

Working with NSArray in Obj C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am currently trying to make a simple craps app for the iPhone.
In the model I have a method:
-(NSString *)resultsOfRoll:(int)firstRoll :(int)secondRoll
{
NSString *results = #"";
NSArray *naturalNumbers = #[#7,#11];
NSArray *losingNumbers = #[#2, #3, #12];
NSArray *pointNumbers = #[#4,#5,#6,#8,#9,#10,#11];
int sum = firstRoll + secondRoll;
if(sum in naturalNumbers)
{
return #"You rolled a natural! You won";
}
return results
}
But I am getting an error. I am pretty new to Obj C and I haven't used much enumeration(If that is wrong please correct me) yet. Could someone let me know if I am initializing the loop correctly and how I can check to see if the sum (roll + roll) is in an array?
Also, does my method name look correct? I am coming from Java and these method sigs are still a little confusing for me.
if(sum in naturalNumbers) obviously isn't valid syntax. You're using something like 'for in' and with an int instead of an object.
What you want is:
if ([naturalNumbers containsObject:#(sum)]) {
Which asks the array if it contains an NSNumber instance containing the sum.
Your method doesn't name the second parameter, which is legal but not encouraged. It would be better as:
-(NSString *)resultsOfRoll:(int)firstRoll secondRoll:(int)secondRoll

Can the ternary operator be used outside of assignment statements in Objective-C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know setting a variable conditionally is valid:
someProperty = [anObject aFunctionThatReturnsBool]? #"Yes" : #"No";
This question has an answer that details the use of ternary statements in preprocessor macros
#define statusString (statusBool ? #"Approved" : #"Rejected")
and within a string formatting method
[NSString stringWithFormat: #"Status: %#", (statusBool ? #"Approved" : #"Rejected")]
But what about within any method?
[NSNumber numberWithInt:(aVariableThatCouldBeSet)? 100 : 0)];
And conditional method calling?
[anObject aFunctionThatReturnsBool]? [self doThis] : [self doThat];
Bonus points for any other uses not listed.

NSInteger is NSInteger error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
-(IBAction)button:(id)sender{
If (cardsinplay >= 16){
NSNumber *cardValue = carsAndValue[14];
NSInteger *Value = [cardValue integerValue[;
From what i know this should read the NSNumber from the array and change it into a NSInteger
But i get this error.
Incompatible integer to pointer conversion initialization 'NSInteger*'(aka 'int*') with an expression of type 'NSInteger'(aka 'int*')
-[NSNumber integerValue] returns a NSInteger, not a pointer to one.
NSInteger *Value = [cardValue integerValue];
should be
NSInteger value = [cardValue integerValue];