Unable to manage single elements in an NSMutableArray - ios

So I am inserting a class object into an NSMutable array, and then I am trying to manage the specific properties of each indexed object.
Here I create the NsMutableArray:
self.currentPuzzle = [[NSMutableArray alloc]init];
for(int i = 0; i < [temp length]; i++){ //temp is a string containing #'s and dots.
AnswerSpecifier *objectToGetIntoArray = [[AnswerSpecifier alloc]init];
objectToGetIntoArray.value = [[temp substringWithRange:NSMakeRange(i, 1)] intValue];
[self.currentPuzzle addObject:objectToGetIntoArray];
}
and here I am trying to change the value property of each object. value is an int property.
for (int i = 0; i < [self.board.boardString length]; i++) {
[self.currentPuzzle objectAtIndex:i].value = [[self.board.boardString substringWithRange:NSMakeRange(i, 1)] intValue];
}
However I get an error saying that the property value was not found on object of type id. Should I just create a new array and assign it to the old one? Is there a better way to do this?

Split your code up. This makes it easier to read and debug.
for (int i = 0; i < [self.board.boardString length]; i++) {
AnswerSpecifier *answer = self.currentPuzzle[i];
NSString *boardString = [self.board.boardString substringWithRange:NSMakeRange(i, 1)];
int value = [boardString intValue];
answer.value = value;
}
You don't get points for cramming as much code as possible into one line.
But if you really just want to fix the one line you need to put the cast in the proper place:
[(AnswerSpecifier *)self.currentPuzzle[i] setValue:[[self.board.boardString substringWithRange:NSMakeRange(i, 1)] intValue]];

Try type casting the object
((AnswerSpecifier *)[self.currentPuzzle objectAtIndex:i]).value = [[self.board.boardString substringWithRange:NSMakeRange(i, 1)] intValue];

Try to replace with:
for (int i = 0; i < [self.board.boardString length]; i++) {
[[self.currentPuzzle objectAtIndex:i] setValue:[[self.board.boardString substringWithRange:NSMakeRange(i, 1)] intValue]];
}

Related

Copy specific index value of array to another array?

I am trying to copy a specific values at specific index from one array to another like this:
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
arrayState is NSMutableArray and arrayTempState is NSArray
but arrayState is null every time.
I tried arrayState[i] = arrayTempState[i]; but it did not work.
arrayState was not initialised that is why it couldn't store the value.
plz try this
arrayState = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}

NSMutableArray with int values from 1 to 100

This should be dead easy, but somehow it doesn't want to work for me. Using iOS 7 and XCode 5.
All I'm trying to do is create an array with values from 1 to 100.
NSMutableArray *array;
for (int i = 0; i < 100; i++)
{
[array addObject:i];
}
This doesn't work. I get a "Implicit conversion of 'int' to 'id' is disallowed with ARC.
I get it, I can't add primitive types to an NSMutableArray.
[array addObject:#i];
This doesn't work either. I get a "unexpected '#' in program"
[array addObject:[NSNumber numberWithInt:i]];
[array addObject:[NSNumber numberWithInteger:i]];
(either case) This "works" (compiles) but it really doesn't "work". The problem with this is that the value from NSNumber is really not a 1-100. What I get for each row is "147212864", 147212832", "147212840"...not what I want.
Lastly:
for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)
{
[array addObject:i];
}
This also doesn't compile. I get an error on the i++. "Arithmetic on pointer to interface 'NSNumber', which is not a constant size for this architecture and platform"
Any suggestions on how to do this extremely simple thing on obj-c?
Either one of these should work:
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
[array addObject:#(i)];
}
or
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
[array addObject:[NSNumber numberWithInt:i]];
}
Here are the reasons why your code snippets did not work:
[array addObject:i] - You cannot add primitives to Cocoa collections
[array addObject:#i] - You forgot to enclose the expression i in parentheses
NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++ - You cannot increment NSNumber without "unwrapping" its value first.
If memory serves, I think you're simply missing parenthesis around the NSNumber shorthand expression.
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
[array addObject:#(i)];
}
Minimally, #i should be #(i) as described here. You are also forgetting to allocate and initialise your array
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++) {
[array addObject:#(i)];
}
And since you are getting: "147212864", 147212832", "147212840"...not what I want., I think you are probably printing out your information wrongly or because the array is unallocated, that's simply garbage. Can you show us how you are outputting?
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *myNum;
for (int i = 0; i < 100; i++) {
myNum = [[NSNumber alloc]initWithInt:i];
[array addObject:myNum];
}
NSLog(#"%#", array); // 1 - 99 as expected
Worked for me :)
Just saying: Turn on all reasonable warnings in your Xcode project. Then read what the warnings are saying and do something about them. When you write something like
for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)
What does a for loop do? An object is in the end a pointer. So you initalise i to nil. Then you compare a pointer with a random pointer: [NSNumber numberWithInteger:100] returns a pointer to an object which could be anywhere in memory, and you compare pointers. Next the i++: No, you can't increment a pointer to an NSNumber. It doesn't make sense.

How can I access a part of this print out

Not sure exactly how to word this but basically I am doing this:
unsigned int propCount = 0;
objc_property_t *properties = class_copyPropertyList([self class], &propCount);
for(int idx = 0; idx < propCount; idx++) {
objc_property_t prop = *(properties + idx);
NSString *key = #(property_getName(prop));
NSString *key2 = #(property_getAttributes(prop));
NSLog(#"%#::%#", key,key2);
}
and it prints out
describeOther::T#"UITextField",&,N,V_describeOther
how can I assign "UITextField" to something or is it already assigned and I can access it like key2[1] or something??
EDIT:
I guess I could do a if statement with a contains constraint but not sure if thats the "cleanest" way to do it.
Get the range of the two double-quotes and extract the substring between those two ranges.
NSRange openQuote = [key2 rangeOfString:#"\""];
NSRange closeQuote = [key2 rangeOfString#"\"" options:NSBackwardsSearch];
NSUInteger start = openQuote.location + openQuote.length;
NSUInteger end = closeQuote.location;
NSRange nameRange = NSRangeMake(start, end - start);
NSString *name = [keys substringWithRange:nameRange);

Appending a string inside for loop crashes ios device

I'm gathering data to an array in string format and one item is about 30 characters. When data collection is finished I try to combine all the strings into one big string, which is then written to a file. Combining strings is done inside for-loop, and it causes device to crash when number of data items gets somewhere over 4000. What is causing it and how to fix? Here's the code I have for appending strings:
NSString *content = #"";
for (int i=0; i<self.log.count; i++)
{
content = [[content stringByAppendingString:#""] stringByAppendingString:(self.log)[i]];
}
If you are trying to turn an array into a string there is an easier way to do it:
NSString *content = [self.log componentsJoinedByString:#" "];
try this...
NSString *content = #"";
for (int i=0; i<self.log.count; i++)
{
content = [[content stringByAppendingString:#""] stringByAppendingString:[NSString stringWithFormat:#"%#",(self.log)[i]]];
}
To ensure the required amount of memory is allocated, I suggest you use a mutable string with an init for the length required.
-(NSString*)concantString:(NSArray *)incomingLog {
int calculatedLength = 0;
for (int i=0; i < [incomingLog count]; i++)
{
calculatedLength += [incomingLog[i] length];
}
NSMutableString *content = [[NSMutableString alloc] initWithCapacity:calculatedLength];
for (int i=0; i < [incomingLog count]; i++)
{
content = (NSMutableString*)[[content stringByAppendingString:#""] stringByAppendingString:incomingLog[i] ] ;
}
return content;
}

Redefinition with a different type

I'm having trouble with this block of code:
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
That last line throws an error:
"Redefinition of 'tempContact' with a different type
initWithJSONData: accepts NSDictionary
andDelay: int
I've tried to rewrite this code, with different types and all,
I'm just not sure what I'm doing
You already declared a variable in this scope named tempContact (NSArray *tempContact...). Change the name of one of them.
NSArray *tempContact and FlokContact *tempContact have the same name, that's the problem.
Change FlokContact *tempContact to FlokContact *temp_Contact or what you want.
try this
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
{
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
}

Resources