How to remove bytes from the end of NSMutableData object - iphone

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

Related

Putting objects from one NSMutableArray to another NSMutableArray

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

Extracting Data from NSMuableDictionary to NSString and compering to String issue

Im Extracting Data from NSMuableDictionary to NSString and try compering to String like this:
NSDictionary *error = [[NSDictionary alloc]init];
NSString *errorCode = [[NSString alloc]init];
error = [sing.globalCallsDitionary valueForKey:#"Error"];
NSLog(#"error code %#",[error valueForKey:#"error_code"]);
errorCode = [error valueForKey:#"error_code"];
if ([errorCode isEqualToString:#"-1"]) {
When the if statement executed i get this error talking about an array, the error looks like this:
2012-04-27 06:34:49.686 CallBiz[10319:707] error code (
"-1"
)
2012-04-27 06:35:00.602 CallBiz[10319:707] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10
2012-04-27 06:35:00.608 CallBiz[10319:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10'
*** First throw call stack:
(0x3541f88f 0x36846259 0x35422a9b 0x35421915 0x3537c650 0xb2a3 0x353793fd 0x32458faf 0x32458f6b 0x32458f49 0x32458cb9 0x324595f1 0x32457ad3 0x324574c1 0x3243d83d 0x3243d0e3 0x3667222b 0x353f3523 0x353f34c5 0x353f2313 0x353754a5 0x3537536d 0x36671439 0x3246be7d 0x2e61 0x28fc)
terminate called throwing an exception
It is as xCode is looking on my NSString *errorCode = [[NSString alloc]init]; as if it is an NSArray, can someone help me with this?
Error occurs because your errorCode came as a array, instead of string. If you are sure that only 1 object come here, then you can use -
if ([[errorCode objectAtIndex:0] isEqualToString:#"-1"])
It will work, although it will through a warning, because errorCode is defined as a string object. So you ned to sure what data you are getting in response and then define data structure appropriately.

NSArray compontentsSeperatedByString returns unrecognized selector

I have a string containing three words, seperated by a pipeline ( | )
I want to break these up into three separate strings
NSArray *bits = [word.variants componentsSeparatedByString: #"|"];
NSLog(#"BITS: %#", bits);
This returns an unrecognized selector. I use this line of code in other areas in my project, and it works fine. But not on this one.
-[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance 0x6dbfa80
Any ideas?
I have also same problem while my string having so many white character , new line character so i cant do anything but finally i got solution as per following:
NSString *artwork = [currentURL valueForKey:#"artwork_large"];
//i got the string artwork ,which is fetch from json.
[smg addObject:artwork];
// add this string to 0th index of an array name:smg
NSSet *setObj1 = [NSSet setWithArray:smg];
//make the nsset for my array (named :smg)
NSString *pictureName = [[setObj1 allObjects] componentsJoinedByString:#","];
//make the string from all the sets joined by ","
picArray = [pictureName componentsSeparatedByString:#","];
//now its time for normal operation means makes the array (name: picArray) from string by componenet separatedbystring method
this way now i got the perfect array which is in our control
You didn't give us the whole error message, but my bet: You are overreleasing either word or variants and therefor the message is received by another object, that doesn't have the method mentioned in the selector. Try NSZombieEnbled. You will find enough post about it on StackOverflow.
edit
The error posted by you fits to my assumption. The only other explanation: variants is a NSArray.
Make it as following, assuming variants as NSString
NSString *lvariant = word.variants;
NSArray *bits = [lvariant componentsSeparatedByString: #"|"];

Issue with Calling Function

I have a method as follows use to correct empty values in Json
+(NSString *)CorrectJsonForEmptyValues:(NSString *)pasRawJson
{
NSLog(#"CorrectJsonForEmptyValues");
NSMutableString *tmpJson = [pasRawJson mutableCopy];
[tmpJson replaceOccurrencesOfString:#"[,"
withString:#"[{\"v\": \"N/A\",\"f\":\"N/A\"},"
options:0
range:NSMakeRange(0, [tmpJson length])];
[tmpJson replaceOccurrencesOfString:#",,"
withString:#",{\"v\": \"N/A\",\"f\":\"N/A\"},"
options:0
range:NSMakeRange(0, [tmpJson length])];
NSString *correctedJson=tmpJson;
return correctedJson;
}
Called the function like this
result = [self performSelector:#selector(CorrectJsonforEmptyvalues:) withObject:result];
But getting error
2011-11-11 11:11:33.217 HelloWorld10[38833:207] -[Data CorrectJsonforEmptyvalues:]: unrecognized selector sent to instance 0x5725cc0
2011-11-11 11:11:33.219 HelloWorld10[38833:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Data CorrectJsonforEmptyvalues:]: unrecognized selector sent to instance 0x5725cc0'
If any one can please provide a solution it will be helpful.
Thanks in advance.
You have declared CorrectJsonForEmptyValues: as a class method by starting its declaration/definition with a + instead of a -. Therefore you call it on the class object, not on an instance of the class. If your class is named Data, for example, you call it like this:
result = [Data CorrectJsonForEmptyValues:result];
By the way, you should not start method names with capital letters.
You can call function as follows
[self CorrectJsonForEmptyValues:result];
And replace the '+' in the following with '-'
+(NSString *)CorrectJsonForEmptyValues:(NSString *)pasRawJson{

Why an NSException error?

I have put the following code in...;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
#"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %#",field.text,
kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
...and I receive an NSException error saying;
*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate:
<NSInvalidArgumentException> +[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did
you forget to nil-terminate your parameter list?
What does this mean? What do I have to do to fix this issue?
Thanks,
James
You are trying to format a string in your dictionary initialization and it expects the format to be object, key, object, key, etc... To fix try creating your formatted string on another line for clarity and then adding it as part of the objects and keys as so
NSString *message = [NSString stringWithFormat:#"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %#",
field.text];
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
#"text/plain", kSKPSMTPPartContentTypeKey,
message, kSKPSMTPPartMessageKey,
#"8bit", kSKPSMTPPartContentTransferEncodingKey,nil];
Maybe you meant something like this:
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain", kSKPSMTPPartContentTypeKey, [NSString stringWithFormat:#"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %#",field.text], kSKPSMTPPartMessageKey, #"8bit", kSKPSMTPPartContentTransferEncodingKey,nil];
You are missing an argument. I count 8 total arguments including nil. That means one of your key value pairs is not complete.
Try firsty create string:
NSString *partMessageKey = [NSString stringWithFormat:#"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %#",field.text];
then put this string to dictionary as object.