can't make addition in NSMutableArray obectatindex - iphone

for(int i=0; i<[arrMaintenanceDetail count]; i++){
for(int j=0; j<[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] count]; j++){
if([[arrMaintenanceDetail objectAtIndex:i] isEqualToString:[[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"]objectAtIndex:i] stringValue]]){
[arrTotalValues objectAtIndex:i] += [arrTotalValues insertObject:[[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue] atIndex:i];
}
}
}
I want to add (make addition of integer values) value from
[[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue];
Whenever loop changes its value.
I want new value from objectAtIndex:j to be added to its previous value and store it for future use.
I will have 4 objects in arrTotalValues (NSMutableArray).
So what should I do ??

You cannot keep integers in NSMutableArray. Use NSNumber to keep integer values in your NSMutableArray as objects.
You didn't provide enough information to give you 100% working code, but try something like that:
NSMutableArray* arrTotalValues = [NSMutableArray array];
for(int i=0; i<[arrMaintenanceDetail count]; i++)
{
NSNumber* totalValue = [NSNumber numberWithInt:0];
for(int j=0; j<[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] count]; j++)
{
if([[[arrMaintenanceDetail objectAtIndex:i] stringValue] isEqualToString:[[[arrMaintenanceCategory valueForKey:#"maintenanceCategory"] objectAtIndex:i] stringValue]])
{
int newTotalValue = [totalValue intValue] + [[[arrMaintenanceCategory valueForKey:#"cost"] objectAtIndex:j] integerValue];
totalValue = [NSNumber numberWithInt:newTotalValue];
}
}
[arrTotalValues addObject:totalValue];
}

Related

About NSMutableArray add NSArray issue

I want to below effect,but I don't kown how to use NSMutableArray combine NSArray More than two?
1.my code
for (int i=0; i<[DateSortArry2 count]; i++) {
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[Arry addObject:sectiondateStr2];
}
}
[SumArry addObjectsFromArray:Arry];
[Arry removeAllObjects];
}
2.my code Result
SumArry:(
"20130227",
"20130227",
"20130227",
"20130226",
"20130226",
"20130226",
"20130225",
"20130225")
3.I want the results
SumArry:((
"20130227",
"20130227",
"20130227",
),
(
"20130226",
"20130226",
"20130226",
),
(
"20130225",
"20130225"
))
Your code repeatedly fills and empties the same array by adding its elements, but you need to preserve the structure with additional instances of NSArray. So, use a new NSArray for each section.
for (int i=0; i<[DateSortArry2 count]; i++) {
NSMutableArray *section = [NSMutableArray array];
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[section addObject:sectiondateStr2];
}
}
[SumArry addObject:section];
}
You can either store a reference to another array (or any type of object) in your array:
[myMutableArray addObject:otherArray];
Or concatinate the arrays.
[myMutableArray addObjectsFromArray:otherArray];
Both of which are documented in the documentation. By the looks of it the first approach is what you want since you want to have NSArray of NSMutableArray.
try this:
please tell me if it works.
thanks
NSString *str = #"";
for (int i=0; i<[DateSortArry2 count]; i++)
{
if (str isEqualToString:[DateSortArry2 objectAtIndex:i])
{
return;
}
else
{
NSMutableArray * Arry = [[NSMutableArray alloc] init];
str = [DateSortArry2 objectAtIndex:i]
for (int j=0; j<[DateSortArry2Copy count]; j++)
{
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:str])
{
[Arry addObject:str];
}
}
[SumArry addObject:Arry];
[Arry removeAllObjects];
}
}

How to position the untitled section in a UITableView

At present if the sortdescriptor is having nil or empty values is being placed in an untitled section which is being placed at the top of the table. I want it to be at the end of the table. Any suggestions?
yes, it is so easy, jst perform a segmentation in which start by the charecter A and check upto z, (or whatever your requiremtn) if it matches nothing, then add it to last array that you are going to show in untititled objects. i have this for contacts. see if it is understandable by u
int numContacts=[cList count];
//NSMutableArray *nonAlphaArray=[[NSMutableArray alloc] init];
NSMutableArray *arrayCollection[27];
for (int i=0; i<27; i++) {
arrayCollection[i]=[NSMutableArray array];
}
for (int i=0; i<numContacts; i++)
{
Contact *contact= [cList objectAtIndex:i];
unichar alphaSmall='a';
unichar alphaBig='A';
unichar first=0x0000;
if([contact.mContactName length]>0)
first= [contact.mContactName characterAtIndex:0];
for (int j=0; j<26; )
{
if (first==alphaSmall || first==alphaBig)
{
[arrayCollection[j] addObject:contact];
break;
}
alphaSmall++;
alphaBig++;
j++;
if (j==26) {
[arrayCollection[26] addObject:contact];
}
}
}
for (int i=0; i<27; i++)
{
[alphaDictionary setObject:arrayCollection[i] forKey:[NSString stringWithFormat:#"%d",i]];
}

NSMutableArray shuffle issue

I have solved the problem with including terminating method.
If(indexes==0){
endUp = YES;
}
Thanks for pointing me the right direction. It wasn't problem of the shuffle.
Alessign
the error may be in the loop you use to shuffle the first array.
You don't post that part of your code...is it something like this?
for (int i=0; i<[indexes count]; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this may be better:
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
// (...)
[indexes removeObjectAtIndex:index];
// (...)
}
this complete code works well, with no errors or crashes:
int length = 10;
NSMutableArray* indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<10; i++) [indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray*shuffle = [[NSMutableArray alloc] initWithCapacity:length];
int arrayCount = [indexes count];
for (int i=0; i<arrayCount; i++){
int index = arc4random()%[indexes count];
NSLog(#"___index: %i", index);
NSLog(#"indexes: %# ", indexes);
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
NSLog(#"shuffle: %# ", shuffle);
}
for (int i=0; i<[shuffle count]; i++){
int questionNumber = [[shuffle objectAtIndex:i] intValue] + 1;
NSLog(#"questionNumber: %i ", questionNumber);
}
I see, anyway, Got it! I implemented one 'if' statement which will terminate it!
if ([indexes count] == 0)
{endProcess = YES}

Problem comparing two arrays

i have a loop in Objective C and i compare two index of the same array but the comparison doesnt work ,there is no compile error. Variable iMinor is always zero and arrayDistancias is not empty:
NSMutableArray *arrayDistancias = [NSMutableArray array];
NSNumber *lat1 = [NSNumber numberWithFloat:39.0025];
NSNumber *lat2 = [NSNumber numberWithFloat:40.2710];
NSNumber *lat3 = [NSNumber numberWithFloat:38.5316];
NSNumber *lat4 = [NSNumber numberWithFloat:27.4529];
NSNumber *lng1 = [NSNumber numberWithFloat:-1.5139];
NSNumber *lng2 = [NSNumber numberWithFloat:-3.4327];
NSNumber *lng3 = [NSNumber numberWithFloat:-7.0029];
NSNumber *lng4 = [NSNumber numberWithFloat:-15.3432];
//for (NSInteger i = 0; i < 40; i++)
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat1 :longitudaqui :lng1]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat2 :longitudaqui :lng2]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat3 :longitudaqui :lng3]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat4 :longitudaqui :lng4]]];
int iMinor = 0;
for (int i = 0; i < [arrayDistancias count]; i++) {
if([arrayDistancias objectAtIndex: i] < [arrayDistancias objectAtIndex: iMinor]){
iMinor = i;
}
}
thanks in advance
[arrayDistances objectAtIndex: i] retuns the NSNumber object, not the integer value. In order to get the integer value use intValue method on the returned object. So, you should compare it like this.
if ([[arrayDistances objectAtIndex: i] intValue] <
[[arrayDistances objectAtIndex: iMinor] intValue]) {

What's wrong with how I'm accessing NSSet from my ModelObject?

I've been trying to get to grips with Core Data and I'm having some difficulty with my fetched results. The problem is that I have 'Branch' objects which have a to-many relationship to 'Telephone' objects. When I return a 'Branch' and try to access all related 'Telephone' objects from the returned NSSet, I seem to only get one object back. See the code below which I'm using to try to see what's going on.
/***************************************************************************************
start testing the fetched objects
***************************************************************************************/
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
for (int i=0; i<[results count]; i++) {
NSString *sortcode = [[results objectAtIndex:i] valueForKey:#"sortcode"];
NSString *lat = [[results objectAtIndex:i] valueForKey:#"latitude"];
NSString *lon = [[results objectAtIndex:i] valueForKey:#"longitude"];
NSSet *phoneSet = [[results objectAtIndex:i] valueForKey:#"telephone"];
int phoneCount = [phoneSet count];
NSArray *phoneArray = [self arrayFromSet:[[results objectAtIndex:i] valueForKey:#"telephone"]];
for (int j=0; j<[phoneArray count]; j++) {
NSString *phoneNumber = [[phoneArray objectAtIndex:j] valueForKey:#"number"];
NSLog(#"%#,%i,%i",phoneNumber,[phoneArray count],phoneCount);
}
NSLog(#"%#,%#,%#",sortcode,lat,lon);
}
/***************************************************************************************
finish testing the fetched objects
***************************************************************************************/
This results in the following output:
2010-03-05 22:05:18.566 AIB[3175:207] 059 9151727,1,1
2010-03-05 22:05:18.566 AIB[3175:207] 933325,52.802288,-6.737655
Here's where I add the objects to the context:
// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
[newTelephone setBranch:newBranch];
[newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:#"number"]];
[newBranch addTelephoneObject:newTelephone];
NSLog(#"i=%i and phone number=%#", i, [newTelephone valueForKey:#"number"]);
}
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
NSLog(#"Save failed with error %#",error);
} else {
NSLog(#"Save was successful");
}
...and here's the output of the above
2010-03-05 22:15:03.217 AIB[3175:6837] i=0 and phone number=059 9151204
2010-03-05 22:15:03.218 AIB[3175:6837] i=1 and phone number=059 9151179
2010-03-05 22:15:03.218 AIB[3175:6837] i=2 and phone number=059 9151727
2010-03-05 22:15:03.231 AIB[3175:6837] Save was successful
The problem is in this code:
// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
[newTelephone setBranch:newBranch];
[newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:#"number"]];
[newBranch addTelephoneObject:newTelephone];
NSLog(#"i=%i and phone number=%#", i, [newTelephone valueForKey:#"number"]);
}
The newTelephone object is the SAME for every iteration of the for loop.
Thus, this line is only adding one object to the set (it is added multiple times, but an NSSet doesn't allow duplicates and that's really not what you want anyway):
[newBranch addTelephoneObject:newTelephone];