tags:

views:

57

answers:

6

hi, I have a const char *sadgan[4]={"yeksad" ,"devist","sisad","chaharsad","pansad"}; How can I append a sadgan[1] to test in objective c?

    NSString *myString =@"";
    NSString *test = [myString stringByAppendingString:sadgan[1]];
    NSLog(@"% ? " ,test);

I want to write yeksad o devist o sisad o ...

+1  A: 

Convert a C Sting to an NSString:

NSString *myNSString = [NSString stringWithUTF8String:cString];

and then append.

ennuikiller
+1  A: 

char* and NSString are not converted to each other automatically. If you use Objective-C, use NSString unless some library you use forces you otherwise. "abc" is a char*, @"abc" is an NSString.

NSArray* sagdan=[NSArray arrayWithObjects:@"yeksad",@"devist",@"sisad",@"chaharsad",@"pansad",nil];
NSString *test = [myString stringByAppendingString:[sagdan objectAtIndex:4]];

will do the job.

By the way, what's the language you used? dev for 2, chahar for 4 and pan for 5 sound like an Indo-European language...

Yuji
thank you , i'm beginner in objective c and it's my first source code :)it's persian.yeksad=100 devist=200 ,...yek=1 do=2 se=3 chahar=4...
aden
Now I know the first few digits in Persian, thank you:)
Yuji
A: 

Off-topic for Objective-C but on-topic for C:

*sadgan[4]={"yeksad" ,"devist","sisad","chaharsad","pansad"};

I don’t know this language, but I can’t help: 1: yeksad, 2: devist, 3: sisad, 4: chaharsad, 5: pansad. Which one do you think will be droped by the array, capable holding 4 values?

Greetings

Objective Interested Person
A: 

@ objective Interested person:

in c:

  const char * array_1[4]={"one","two","tree","four","five"};
  printf("%s",array_1[4]) //output= five

in objective_c:`

 NSString *mystring=@"";

 NSArray* array_1=[NSArray arrayWithObjects:@"one",@"two",@"tree",@"four",@"five",nil];

 NSString *test = [mystring stringByAppendingString:[array_1 objectAtIndex:4]];  

 NSLog(@"%@",test);     //output =five
aden
your declaration of `const char * array_1[4]` is wrong. It should be `[5]`.
Dave DeLong
why?? in c array rang is (0 .. 4 )= 5
aden
A: 

@aden: Sure, the array data is placed in the heap. This is done in the usual way, so you can acess it. But it is not secure: it depends on the way, the compiler is actually handling such things. And: what if the array is not located on the heap? Maybe on the stack? Yes, in most cases this will be a bad idea, but it is still an idea.

A real bad idea is the way to think “it’s just working, so don’t worry about it”. Don’t make unneccessary assumptions.

Greetings

Objective Interested Person
A: 

@aden: “why?? in c array rang is (0 .. 4 )= 5” — looks as you should have a few lessons in C first? something array[value]; is reserving memory space. Space for value “somethings”. You just know, the index starts with 0. So just try out: if the first element is called 0, what is the index for the last one if there are value elements?

Greetings

Objective Interested Person