Can't Assign Expression in iOS [closed] - ios

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] = ...

Related

Objective C - better way to write my if statement [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 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.

Mapping multiple Arrays in one Array [closed]

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 do have two Arrays one array with list of items:
One array with string objects
and one with Array objects
NSArray * a=[1,2,nil];
NSArray * b=[[abc],[def],[ijk],[lmp], nil];
I want to return Array by mapping Array "a" object "1" to array "b" with objects "[abc],[def]"
and Array "a" object "2" to array "b" with objects "[ijk],[lmp]"
I know I can achieve it in NSDictionary but i want to return NSArray not NSDictionary.
or any alternative method to do it.
I think it's more about data structures knowledges?
I see that your pattern is to increase two index.
So, it's something you want?
NSMutableArray *object;
NSUInteger indexOfA = [a indexOfObject:#1]; // get the index of the object from A
for (NSUInteger index = 0; index < 2; index++) {
[object addObject:b[indexOfA * 2 + index]];
}
Then, you can pull out what you want from b.

NSNotFound comparison not working [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 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.

How to combine two array in ios [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
Array1 : { A , B , C }
Array2 : { 1 , 2 , 3 }
I need to combine of this array list like:
Combine array :
{
A(1),
B(2),
C(3)
}
How can I implement this?
you can use NSDictionary
NSDictionary *dict = [NSDictionary dictionaryWithObjects:array1
forKeys:array2];
NSString *value = dict[#"1"]; //value = #"A";

compare last 6 or 8 character of nsstring into some other nsstring [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
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!");
}

Resources