Redefinition with a different type - ios

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];
}
}

Related

How can I add in multiple items in model by using array method?

I have an original code as below which is working properly:-
- (ZYSideSlipFilterRegionModel *)commonFilterRegionModelWithKeyword:(NSString *)keyword selectionType:(CommonTableViewCellSelectionType)selectionType {
ZYSideSlipFilterRegionModel *model = [[ZYSideSlipFilterRegionModel alloc] init];
model.containerCellClass = #"SideSlipCommonTableViewCell";
model.regionTitle = keyword;
model.customDict = #{REGION_SELECTION_TYPE:#(selectionType)};
model.itemList = #[[self createItemModelWithTitle:[NSString stringWithFormat:#"Local"] itemId:#"0" selected:NO],
[self createItemModelWithTitle:[NSString stringWithFormat:#"Oversea"] itemId:#"1" selected:NO]];
return model;
}
Now, I plan to change the static value (Oversea / local) to dynamic value. But only 1 item will be displayed.
for (int i = 0; i < filteredArray.count; i++) {
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:[filteredArray[i] valueForKey:#"attribute_name"] itemId:myNewString selected:NO] ];
}
How can I put 2 item in model.itemList? Please help. Thank you.
You can use this way
//step:1 fetch Dictionary like this
for (int i = 0; i < filteredArray.count; i++)
{
NSMutableDictionary *dict = (NSMutableDictionary *)filteredArray[i] ;
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:dict];
}
//Step 2 : You can define your method for Model Like this
- (CommonItemModel *)createItemModelWithTitle:(NSMutableDictionary *)dictModel
{
CommonItemModel *model = [[CommonItemModel alloc] init];
model.itemId = [dictModel valueForKey : #"itemId"];
model.itemName = [dictModel valueForKey:#"itemTitle"];
model.selected = [dictModel valueForKey:[NSNumber numberWithBool:
[[dictModel valueForKey:#"selected"]]]];
return model;
}
//One more thing you are write this in first step
model.itemList = #[[self createItemModelWithTitle:dict];
But the method only return the Model class (CommonItemModel) so if you need any help you shared here
Thanks :)

Objective-c program crashes when adding object to NSMutableArray

So, I'm kind of new to Obj-c, and I have a strange crash happening with the following code :
- (NSMutableArray*)followNonBlackPixels:(int)startX withY:(int)startY
{
NSMutableArray* result;
NSMutableArray* adjacents = [self getAdjacents:startX withY:startY];
int r = 0;
int i = 0;
int tempX;
int tempY;
int max = [adjacents count];
CGPoint tempPoint;
while(i < max)
{
int tempX = (int)[[adjacents objectAtIndex:i] CGPointValue].x;
int tempY = (int)[[adjacents objectAtIndex:i] CGPointValue].y;
result = [self getAdjacents:tempX withY:tempY];
for(r = 0; r < [result count]; r++)
{
tempPoint = [[result objectAtIndex:r] CGPointValue];
//[adjacents addObject:[NSValue valueWithCGPoint:CGPointMake(tempPoint.x, tempPoint.y)]];
}
i++;
max = [adjacents count];
}
return adjacents;
}
This code runs fine, but as soon as I uncomment the line where I add an object to the "adjacents" NSMutableArray, then the program crashes.
The signature of the getAdjacents method is as follows:
- (NSMutableArray*)getAdjacents:(int)startX withY:(int)startY;
I'm developing a Cordova plugin under Windows so I do not have any debug info to provide... But maybe my mistake would be clear to an experienced obj-c developer ?
Thanks a lot for you help !
If you change your code to the following,
NSMutableArray *adjacents = [[NSMutableArray alloc] initWithArray:[[self getAdjacents:startX withY:startY] mutableCopy]];

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.

Unable to manage single elements in an NSMutableArray

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]];
}

Resources