How to copy from NSMutableArray to NSMutableString? - objective-c

Im trying to add some strings into NSMutableArray,than want to copy element 0(zero)
from NSMutableArray to NSMutableString
[MS appendString:str]
this line raises error.
what is the problem ?
a MutableArray element cannot be assigned to NSString ?
(thanks for any responses)
error output is :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x6c82440'
here is my code :
NSMutableArray *A=[[NSMutableArray alloc]init];
NSMutableString *MS=[NSMutableString stringWithString:#"first"];
for (int i=0; i<=15;i+=2) {
[A addObject:[NSNumber numberWithInteger:i]];
}
NSString *str;
for (int x=0; x<[A count]; x++) {
str=[A objectAtIndex:0];
[MS appendString:str];//ERROR HERE
}
[A release];

You are adding NSNumbers to the array and not strings n the folioing line:
[A addObject:[NSNumber numberWithInteger:i]]; // here you are adding numbers
So when you read them from the array later, they are still numbers and you have to create strings from them:
NSString *str;
NSNumber *number;
for (int x=0; x<[A count]; x++) {
number = [A objectAtIndex:0]; // You should probably replace 0 by x here.
str = [number stringValue]; // transform number into a string
[MS appendString:str];
}
[A release];
But if you want the array to store strings and not numbers, then replace the first loop instead with the following:
for (int i=0; i<=15;i+=2) {
[A addObject:[[NSNumber numberWithInteger:i] stringValue]];
}
Or
for (int i=0; i<=15;i+=2) {
[A addObject:[NSString stringWithFormat:#"%d", i]];
}

Related

Adding strings to an array in for loop and not overwrite the value in objective-c

I define a NSMutableArray *nameArray in .h file. And add strings using this nameArray in a for loop in .m file like below:
for (int i=0; i<[data count]; i++) {
NSDictionary* place = [data objectAtIndex:i];
NSString *name=[place objectForKey:#"name"];
_nameArray = [[NSMutableArray alloc] init];
[_nameArray addObject:name];
}
NSLog(#"After loop, Name is %#", _nameArray);
Actually, it should have several names in this array. But why it only output one name which was added in the array last?
You must create your array outside the loop, that is your problem.
// Init array
_nameArray = [[NSMutableArray alloc] init];
// Fill array
for (int i=0; i<[data count]; i++) {
NSDictionary* place = [data objectAtIndex:i];
NSString *name=[place objectForKey:#"name"];
[_nameArray addObject:name];
}
// Log array
NSLog(#"After loop, Name is %#", _nameArray);
All of your objects are added to an own array and only the last array will not be overwritten and is therefore in the log.

Error querying filter in Hebrew

I have the following code:
NSMutableArray *getDatosSelect=nil;
NSMutableString *consulta=[[NSMutableString alloc]init];
[consulta appendString:#"SELECT idOzDiccionario, espanyol, ingles, hebreo, fonetica FROM ozdiccionario WHERE UPPER(ingles) LIKE 'FRIEND%'"];
if ( sqlite3_prepare_v2(sql,[consulta UTF8String],[consulta length],&resultado,&siguiente) == SQLITE_OK ){
getDatosSelect=[[NSMutableArray alloc] init];
while (sqlite3_step(resultado)==SQLITE_ROW){
NSMutableArray *datos=[[NSMutableArray alloc] init];
for (int x=0; x<sqlite3_column_count(resultado); x++) {
char *pChar=(char *)sqlite3_column_text(resultado, x);
if (pChar!=nil) {
[datos addObject:[NSString stringWithUTF8String: pChar]];
}
}
[getDatosSelect addObject:datos];
datos=nil;
}
}else {
NSString *errores=[NSString stringWithFormat:#"%s", sqlite3_errmsg(sql)];
NSLog(#"%# %#",TAG,errores);
}
which runs correctly while if I run this other code
NSMutableArray *getDatosSelect=nil;
NSMutableString *consulta=[[NSMutableString alloc]init];
[consulta appendString:#"SELECT idOzDiccionario, espanyol, ingles, hebreo, fonetica FROM ozdiccionario WHERE hebreo='חבר'"];
if ( sqlite3_prepare_v2(sql,[consulta UTF8String],[consulta length],&resultado,&siguiente) == SQLITE_OK ){
getDatosSelect=[[NSMutableArray alloc] init];
while (sqlite3_step(resultado)==SQLITE_ROW){
NSMutableArray *datos=[[NSMutableArray alloc] init];
for (int x=0; x<sqlite3_column_count(resultado); x++) {
char *pChar=(char *)sqlite3_column_text(resultado, x);
if (pChar!=nil) {
[datos addObject:[NSString stringWithUTF8String: pChar]];
}
}
[getDatosSelect addObject:datos];
datos=nil;
}
}else {
NSString *errores=[NSString stringWithFormat:#"%s", sqlite3_errmsg(sql)];
NSLog(#"%# %#",TAG,errores);
}
I get the following error:
unrecognized token: ""◊ó◊ë◊"
This error occurs when the method converts a string to UTF8 character
Why? What is the problem?
You've used [consulta length] as the number of bytes in consulta. That is not correct. length is the number of characters. The number of bytes could be more, and Hebrew generally will be. To get the length, you need to use:
[consulta lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
It is likely that your queries are being truncated, so they may or may not work depending on their exact content.

print array of NSString in Objective-C

So I have a NSMutableArray of size 100, and I want to print 10 cells at a time (So it looks like a grid). What is the easiest way to print 10 indexes at a time? Thanks.
Here's what I did, it only prints 1 index at a time.
-(void) printBoard{
for(int i =0; i<_board.count; i++){
NSLog(#"%# |", [_board objectAtIndex:i]);
}
If you want to print a contiguous array as a two-dimensional array, you can use something like the following:
- (NSString *) description
{
NSMutableString *result = [NSMutableString string];
for (int y = 0; y < 100; y += 10)
{
NSArray *row = [_board subarrayWithRange:NSMakeRange(y, 10)];
NSString *rowString = [row componentsJoinedByString:#" | "];
[result appendFormat:#"%#\n", rowString];
}
return result;
}
If you put this instance method in your class then you can use NSLog() directly on your instance (that is instead of doing [object printBoard] your would use NSLog(#"%#", object);).

NSMutableArray to NSString conversion

Here is the string from NSMutableArray:
(
(
"Some String Value"
)
)
This code displays the string value that I want, but however, it displays with the brackets and quotes. How do I remove them?
Thank you in advance!!
In your case it is 2D Array:
Something like this:
NSArray *arr=[NSArray arrayWithObject:#"asdf"];
NSArray *arr2=[NSArray arrayWithObjects:arr, nil];
You need to go to 2nd level to retrive it as :
NSLog(#"=> %#",arr2[0][0]);
NSString *string=arr2[0][0];
-(void)repeatArray:(NSArray *)array1
{
static NSMutableString *InsideString; // Better to use global Varaible declared in ViewDidLoad/loadView
NSArray *array = array1; //Its your array
for (int i = 0; i< [array count]; i++) {
if ([[array objectAtIndex:i] isKindOfClass:[NSArray class]]) {
[self repeatArray:[array objectAtIndex:i]];
} else if ([[array objectAtIndex:i] isKindOfClass:[NSString class]]) {
[InsideString appendString:[NSString stringWithFormat:#"%# ", [array objectAtIndex:i]]];
}
}
}

NSArray elements from parsed .xml file not accessible on iPhone, using TouchXML

I have an NSMutableArray *myArray, wich is the result of a properly .xml parsed file, using TouchXML.
I just want to extract all elements with key name and store then in a separate NSMutableArray, but my final NSMutableArray *namesList is not accessible, crashing my iPhone app right away, because it only contains the last enumerated string and not the whole array.
Here is the code:
NSMutableArray *namesList = [[NSMutableArray alloc] initWithArray:myArray copyItems:YES];
int i;
for (i = 0; i < [myArray count]; i++)
{
namesList = [[myArray objectAtIndex:i] objectForKey:#"name"];
NSLog(#"All Names: %#", namesList);
}
NSLog(#"First Name: %#", [namesList objectAtIndex:0]); <-- crashing line
And here is the NSLog:
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540
2012-04-04 10:34:07.882 Sections[3610:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540'
You should be able to do this:
NSMutableArray * namesList = [NSMutableArray alloc] init];
for(i = 0; i < [myArray count]; i++)
{
NSString * tempString = [NSString stringWithString:[[myArray objectAtIndex:i] objectForKey:#"name"]];
[namesList addobject:tempString];
}
NSLog(#"First Name: %#", [namesList objectAtIndex:0]);
[[myArray objectAtIndex:i] objectForKey:#"name"]; returns an NSString object and not another array therefore you can't send an objectAtIndex to it. Check the structure of your arrays.