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]);
}
Related
I did post request for retrieving data from the server. Now, I have NSDictionary with certain data. I need to display it in a chart, so I have to convert data to appropriate. Here's data which I get from the server:
dates = (
"01.01.2016",
"01.01.2016",
"01.01.2016"
...)
closes = {
0 = (
1,
1,
1,
...)
}
I need to transform it to this format:
{
"01.01.2016" = 1;
"01.01.2016" = 1;
"01.01.2016" = 1;
...
}
How can I do this?
NSArray *dates = ...
NSArray *closes = ...
NSMutableDictionary *result = [NSMutableDictionary new];
for (int i = 0; i < dates.count; i++) {
result[dates[i]] = closes[i];
}
NSLog(#"%#", result)
Note that dates and closes arrays must have same length
Assuming the number of 'dates' and closes elements are equal:
NSMutableDictionary dataOut = [NSMutableDictionary dictionary];
for (int index = 0; index < dataIn[#"dates"].count; index++)
{
dataOut[dataIn[#"dates"][index]] = dataIn[#"closes"][index];
}
Depending on how certain you are and on how fool proof you need this code to be, you may want to add checks (and error handling) to cover the above assumption.
If you have to 2 array and you need to create dictionary then
_productArray2 & _productArray1
NSDictionary * productDictionary = [NSMutableDictionary dictionary];
for (NSUInteger index =0; index < _productArray1.count; index ++) {
[productDictionary setValue:_productArray1[index] forKey:_productArray2[index]];
}
and if you already have a dictionary want to replace of set value to it
NSArray * allKeys = [productDictionary allKeys];
for (NSUInteger index =0; index < allKeys.count; index ++) {
[productDictionary setValue:_productArray[index] forKey:allKeys [index]];
}
I want to store all the emails into an array. What do I need to do for the same ?
Use this code and change it according to your requirement
NSMutableDictionery * repsonseDict = [[NSMutableDictionery alloc]init];
repsonseDict = [reponseObj objectForKey#"response"];
NSArray * allKeys = [repsonseDict allKeys];
NSMutableArray * emailArr = [[NSMutableArray alloc]init];
for(int i = 0; i < [allKeys count]; i ++ ){
NSString * numberKey = [NSString stringWithFormat:#"%d",i];
[emailArr addObject:[[repsonseDict objectForKey:numberKey]objectForKey:#"email"]];
}
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]];
}
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;
}
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];
}
}