I have 2 NSMutableArrays and i want to put certain object form 1 array to the other array I have already code written but it doesnt work and it gives me this error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 296 beyond bounds [0 .. 295]'
* First throw call stack:
(0x1c9a012 0x10d7e7e 0x1c3c0b4 0x2f04 0xb20b90 0x1c59376 0x1c58e06 0x1c40a82 0x1c3ff44 0x1c3fe1b 0x1bf47e3 0x1bf4668 0x1f65c 0x252d 0x2455)
libc++abi.dylib: terminate called throwing an exception
Initialisierung in viewdidload:
arrayLine1 =[[NSMutableArray alloc] initWithCapacity:80000];
arrayLine1a =[[NSMutableArray alloc] initWithCapacity:70000];
line1tagzahl=0;
line1tagzahl2=0;
passing code:
for (int a=0; a<10; ) {
[arrayLine1a insertObject:[arrayLine1 objectAtIndex:line1tagzahl2] atIndex:line1tagzahl2];
line1tagzahl2=line1tagzahl2+1;
a=a+1;
}
function to create objects in array(this function is called very fast and frequently) :
for (float a=0; a<0.8; ) {
UIImageView *line1 =[[UIImageView alloc] initWithFrame:CGRectMake(Startpoint1.center.x-(w/2),Startpoint1.center.y-kurve1yf+a,w,h)];
line1.image=[UIImage imageNamed:#"Unbenannt"];
line1.tag=line1tagzahl;
[self.view addSubview:line1];
[arrayLine1 insertObject:line1 atIndex:line1tagzahl];
line1tagzahl=line1tagzahl+1;
a=a+0.1;
}
now you should have more information
and if you ask i have more than 10 objects in array2
Seems you haven't read the error message you posted. You have 296 objects in the array. But if you just want to copy the array, why don't you... er... copy it?
NSMutableArray *secondArray = [firstArray mutableCopy];
Is there any problem with using a default method -addObjectsFromArray: of NSMutableArray class?
- (void)serverGotResponse:(NSArray *)objects {
[_myMutableArray addObjectsFromArray:objects];
}
To prevent out of bounds exception, you should use count property:
while (i < array.count) {
// do something ..
i++;
}
if you are copying all the objects, you can just copy the entire array:
array2 = [array1 copy]
You are trying to add the first 10 objects, but your exception was thrown on index 296, which is the first index that comes out of bounds.
For some reason, your while condition is not working properly, so I suggest you start looking there
I also suggest you use a for, it's actually more simple and straightforward than a potential infinite loop
for (int a = 0; a < 10; a++) {
[array1 insertObject:[array2 objectAtIndex:a] atIndex:a];
}
Related
I have a NSMutableData object that is giving me some trouble, I am trying to remove the last 6 bytes from the object like this
NSMutableData *reducedDataPacket = [[NSMutableData alloc] init];
reducedDataPacket = [myCompressedData copy];
NSRange range = NSMakeRange([reducedDataPacket length]-6, 6);
[reducedDataPacket replaceBytesInRange:range withBytes:NULL length:0];
However once the last line executes my app crashes and I am left with this error below.
-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
I have never tried doing this before and have been going off other answeres supplied I have investigated, but I just cannot get this to work... any help would be greatly appreciated.
Your first line is useless because you then redefine reducedDataPacket in the second line, so that first line should be deleted. I'm guessing that myCompressedData is NSData rather than NSMutableData, so change that second line to :
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
First you need a mutable instance, it isn't clear why you create one and then copy it. You should just do:
NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];
Then you want to reduce the length, not try to fill part of the data with nothing:
[reducedDataPacket setLength:(reducedDataPacket.length - 6)];
This is the error I am reciving when I select a value from my UITableView
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_ILLEGAL_ARGUMENT_ERROR (string Motron, pattern
libc++abi.dylib: terminate called throwing an exception
(lldb)
This is the first time I have had an error like this.. I am receiving it in my tableview method didSelectRowAtIndexPath on the second line of code shown below
// This predicate restricts the filterDataArray to the related values of the selected index
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K like %#",#"MASTER",cell.textLabel.text];
filterDataArray = [dataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
I have logged out the array of dictionaries and this is what one of the dictionary values looks like.
{
HASM = 1;
ISM = 0;
ISV = 0;
MASTER = Merc;
MANURE = 96;
}
There are about 60 or 17 of these dictionaries in the array.
any help would be greatly appreciated.
I think you are not correctly using predicate in this case, try this
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"MASTER CONTAINS [c]%#", cell.textLabel.text];
filterDataArray = [dataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
I am developing an application, in which I am getting the values from the URL, for example, take it as (demo.png), here what I am doing is, i am separating the string with <componentsSeparatedByString:#"." > and saving that in the array (Index 0 : demo & Index 1: png ). And, it works fine. Now, what i getting struck here, when the value from the URL doesn't contain ".png, .jpeg" then error message is coming. How can i check whether there is a value in ObjectAtIndex:1 is null. What i did for this was,
Coding :
if ([array45 objectAtIndex:1] == NSNULL NULL) {
NSLog(#"object at index %i has no data", i);
}
Error message:
[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Help me with your valuable solutions.
NSArray *aray = [#"demo.png" componentsSeparatedByString:#"."];
if ([[aray lastObject] isEqualToString:#"png"])
{
NSLog(#"There is an image with .png");
}
else {
NSLog(#"There is no image eith .png extension");
}
First you want to find out how many objects are in the array.
int count = [array count];
If you don't have the correct count you don't check what's in an index.
I am new to IPhone programming and am I having trouble solving the following memory leak.
while(numDeckCounter < numDecks){
int cardCounter=1;
for (int i =1; i<=52; i++) {
tempCard = [Card new]; //leaks tool says that this is leaking object
if(i>=1 && i<=13)
{
tempCard.suit = CLUBS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard]; //reference count 2
cardCounter++;
}
else if(i>=14 && i<=26)
{
tempCard.suit = DIAMONDS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else if(i>=27 && i<=39)
{
tempCard.suit = HEARTS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else
{
tempCard.suit = SPADES;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
if(cardCounter ==14){
cardCounter=1;
}
[tempCard release]; //this causes an EXC_BAD_ACCESS -reference count should be 1
}
numDeckCounter++;
}
I was under the impression that adding an object to the array would increase its reference count by one, then it would be safe to release the object you just added because it would not be deallocated until the array was released bumping which would then release each object in the array. This is when the object should finally be deallocated.
When I add the [tempCard release]; it crashes my app because it can't access the memory location because it has already been deallocated.
From everything I have read, I think what I said above is true. Someone please correct me if I am wrong. Thanks.
I don't see any leaks in the code you presented. (There are some opportunities to slim it down, though, say by moving the identical operations out of the conditionals).
The Leaks tool output is pretty tricky to read. Is it possible that the Card object is leaking one of it's ivars?
Instead of leaks, run static analysis on your product (Product->Analyze). I think it will flag a different part of your code.
Perhaps try [[Card alloc] init] instead of [Card new]. This is just a guess. However trying the IMO more common method of object creation could be helpful.
Check this out: Use of alloc init instead of new
You could also try removing all the code for adding the Cards to the array. So you'd essentially have:
card = [Card new];
[card release];
This could help you find memory issues associated w/ the array retaining the object perhaps?
Why am I getting NSFastEnumerationMutationHandler crash all of a sudden in my code. I am blank why this crash popped up all of a sudden and how to squash it.
Crash Error:
**** Terminating app due to uncaught exception 'NSGenericException', reason: '* **Collection <__NSArrayM: 0x610000859410> was mutated while being enumerated.'*
You must be trying to change an array while you using fast enumeration.
Example
for ( id anObject in anArray ) {
if ( /* anObject satisfies some condition */ ) {
[anArray removeObject:anObject];
}
}
That shouldn't be done. Use a different array or probably filteredArrayUsingPredicate: method to filter. Remedy, however, depends on what you're trying to do.
Came here looking for a solution and ended up taking a copy of the original array to get around the issue.
for (NSObject *object in [array copy]) {
if(condition) {
[array removeObject....]
break;
}
}