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 9 years ago.
Improve this question
Simple one.
I'm using indexOfObject to check if a value is already in an array.
If the value isn't in the array indexOfObject returns the constant NSNotFound.
Why does comparison with > 100000 work:
NSInteger indexOfCell = [_selectedCellIndices indexOfObject:cellIndex];
if(indexOfCell > 1000000)
but equality with NSNotFound fail:
NSInteger indexOfCell = [_selectedCellIndices indexOfObject:cellIndex];
if(indexOfCell == NSNotFound)
2147483647 is NSNotFound (also known, under certain circumstanced, as -1). There is something wrong with how you are testing whether your test succeeds or fails, because Cocoa itself is behaving exactly as advertised.
NSInteger indexOfCell = [[NSArray new] indexOfObject:#""];
NSLog(#"%d", indexOfCell); // some number
NSLog(#"%d", NSNotFound); // the same number
NSLog(#"%d", indexOfCell == NSNotFound); // 1, i.e. YES
Try it yourself.
you could use the ContainsObject: method. Might be easier.
indexOfObject is suppose to be NSUInteger (unsigned) not NSInteger. That is probably screwing up your comparison. Try changing the type to NSUInteger and trying again.
Related
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 8 years ago.
Improve this question
It's not an actual problem but it is frustrating me..
I was looking for a better way to right an IF statement with multiple values that can be accepted.
For example:
if ([[myJson objectForKey:#"pages"] intValue] == 0 || [[myJson objectForKey:#"pages"] intValue] == 3)
Isn't there any way to write something like:
if ([[myJson objectForKey:#"pages"] intValue] == 0 | 3)
{
}
Thanks !!
No, not really. You could do this:
int pages = [[myJson objectForKey:#"pages"] intValue];
if (pages == 0 || pages == 3)
That's what I would recommend. The code you posted is both less efficient and harder to maintain than the code I show.
In your code you actually invoke the objectForKey and intValue methods twice on the same object.
Plus if at some point in the future you change the key value, or the variable name, you have to make the same edit in 2 places, which is more work and adds another chance to introduce a new copy/paste error.
In addition to the other valid answers, you could use a switch:
int numberOfPages = [[myJson objectForKey:#"pages"] intValue];
switch (numberOfPages) {
case 0:
case 3: {
NSLog(#"Is 0 or 3");
break;
}
default: {
NSLog(#"Is NOT 0 or 3");
break;
}
}
This is a much better method as it is clean and easier to read:
int x = [[myJson objectForKey:#"pages"] intValue];
if (x == 0 || x == 3) {
}
You are highly optimistic there.
If myJson is not an NSDictionary your app will crash.
If myJson[#"pages"] is not an NSNumber then your app will crash.
If myJson[#"pages"] does not exist then intValue will return 0.
If myJson[#"pages"] has a value of 0.9 or 3.7 then intValue will return 0 or 3.
I suggest you add a category to NSDictionary with a method like integerValueForKey: withDefault where you lookup an item, check that it is an NSNumber with an integer value, return that value or return the default value.
As David Rönnqvist pointed out, it is mostly a matter of preference. I would personally go with NSArray containing allowed values. This way you won't clutter your code with unnecessary intValue calls. Also adding another allowed value will only require adding a single value to an array instead of adding another condition inside if statement.
Note that you can use Objective-C literal syntax to make the code more concise.
NSArray *allowedValues = #[#0, #3];
if([allowedValues containsObject:myJson[#"pages"]]) {
}
If [myJson objectForKey:#"pages"] is an NSNumber. You could do this:
if([#[#0, #3] containsObject:[myJson objectForKey:#"pages"]])
And if myJson is an NSDictionary you could even shorten it to:
if([#[#0, #3] containsObject:myJson[#"pages"]])
It's not necessarily optimal, but it does provide you with a lot more flexibility when adding more values to check against, which it sounds like you're looking for as opposed to the fastest code. If you're just checking a few values I'm assuming speed of execution is not an issue.
Side note: this works because NSArray's containsObject: method calls isEqual: on every object. NSNumber's isEqualToNumber: gets called. This will also work with NSString instead of NSNumber if that's what you're working with. Just change the search array to hold NSString objects in that case.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to put a number from a textfield on a Parse database and I need to specify that it is a number. How can I do this?
I have done the equivalent with a string using .text
Your question is unclear but I think you mean something like this:
UITextField *textField = ... // some text field
int age = [textField.text intValue];
user[#"age"] = #(age);
You need to convert the string value from the UITextField to a number, (e.g. int, double, or NSNumber).
NSString *textInput = myTextField.text;
int intValue = [textInput intValue];
NSNumber *age = [NSNumber numberWithInt:intValue];
user[#"age"] = age;
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
I have a string *strSearchNumber = #"9970405060" and want to check whether it is present in string *strPhoneMobile = #"+91997040560"
How can I check if last 6 or 8 characters of my string are present in some other string. Thanks in advance.
Simply use -[NSString hasSuffix:].
Use the NSString method hasSuffix:
NSString *strSearchNumber = #"9970405060";
NSString *strPhoneMobile = #"+91997040560";
if ([strPhoneMobile hasSuffix: strSearchNumber]) {
// do something
}
How do I check if a string contains another string in Objective-C?
Better then just checking the end or front of the string. You should check if the string contains the other bit of string. The link above will give you the answer.
Underneath the short version of the answer.
NSString *string = #"hello bla bla";
if ([string rangeOfString:#"bla"].location == NSNotFound) {
NSLog(#"string does not contain bla");
} else {
NSLog(#"string contains bla!");
}
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 8 years ago.
Improve this question
I've got an NSMutableArray called Letters, and it contains instances of a UIImageView subclass. I am trying to loop through them and assign them new values
for (int i=0 ;i<26;i++)
{
[letters objectAtIndex:i] = [[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]];
}
This doesn't work though. Xcode says "Cannot assign to expression", but when I try to assign new values to each array item individually, outside of the array, it works. Do you know why this would be?
EDIT: Nevermind. I'm sorry. I should have looked into arrays a bit more.
The method you're looking for is.-
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
for (int i=0 ;i<26;i+=1) {
[letters replaceObjectAtIndex:i withObject:[[typeLetters alloc]initWithManager:self atX:startX andY:startY withSideLenght:48 andName:[letterNames objectAtIndex:i] andPng:[letterNames objectAtIndex:i]]];
}
array notation works: letters[i] = blah
But you should probably be using fast enumeration:
for(id letter in letters) {
letter = blah;
}
You need to use replaceObjectAtIndex:withObject:, but you should really use the new array syntax:
letters[i] = ...
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
here i am stuck with the following chunk of code
self.isFiltered?[self.filteredCategories count]:[self.categories count]
here isFiltered is an bool, filteredCategories is a mutable array, categories is an array. This line return an integer. I don't understand what and how this line is working. Thanks
That's a ternary statement. Say you have an if like this:
if (condition)
var = one thing
else
var = other thing
As a ternary, that would be
var = condition ? one thing : other thing
So, in your case, it will set your variable to the filter count if filtered, or to the full category count if unfiltered.
It's the same to:
int someVariable = 0;
if(self.isFiltered) {
someVariable = [self.filteredCategories count];
} else {
someVariable = [self.categories count];
}
In your code it's just another form.
Well it's not that hard, it's an "advanced" version of an if/else. It says that if the variable isFiltered is set to YES, it will return the number of elements in the filteredCategories array, but if it is set to NO it will return the number of elements in categories.
You can use a normal if-else to simplify things. When in doubt, go with the standard if-else syntax
if(self.isFiltered){
[self.filteredCategories count]
}
else{
[self.categories count]
}