Store user input in array objective-c - objective-c

i am trying to get input from user and want to store it in an array.
int main(int argc, const char * argv[]) {
#autoreleasepool
{
int i;
char name[10];
NSMutableArray *myarray=[[NSMutableArray alloc]init];
for (i=0; i<10; i++)
{
scanf("%c",name);
[myarray addObject:i];
}
}
return 0;
}

You are trying to insert a non Object in NSMutableArray.
NSMutableArray can store objects only,
char and int are data types of c language which are not treated as objects in Objective C.
First you need to convert them into objects then You can insert.
Try with this:
[myarray addObject:#(i)]; or
[myarray addObject:[NSNumber numberWithInt:i]];
for name:
[NSString stringWithFormat:#"%c",name]

A textfield will hold user input. Fetch value from the textfield and add it to the array.
arryData = [[NSMutableArray alloc] initWithObjects:#"#&", textFieldinput.text, nil];
This will work better.

Related

how to convert const char * argv[] in NSArray in Objective C

I just want to convert the const char * argv[] in main method to NSArray or NSMutableArray. But I am really stuck and couldn't find any solution.
Any help will be appreciated.
You have a C-array of char * (C strings). You need to convert each C string into an NSString. Then you can add each NSString to your NSMutableArray.
int main(int argc, char *argv[]) {
NSMutableArray *results = [NSMutableArray array];
for (int i = 0; i < argc; i++) {
NSString *str = [[NSString alloc] initWithCString:argv[i] encoding:NSUTF8StringEncoding];
[results addObject:str];
}
}

Why am i not able to scan the full string with the spaces?

I try to scan a string and divide it to separate strings.
My code is:
#import <Foundation/Foundation.h>
NSArray *wordsArray;
int amount;
char input[80];
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSString *stra=[[NSString alloc] initWithString:#""];
// stra=#"this is my sentence";
NSLog(#"Please Enter a sentence!");
scanf("%s",&input);
stra=[NSString stringWithCString:input encoding:NSASCIIStringEncoding];
// NSLog(#"words: %#",str);
wordsArray = [stra componentsSeparatedByString:#" "];
amount= [wordsArray count];
NSLog(#"Number of Shows : %d", amount);
for (int i=0; i<amount; i++)
{
NSString *subString = [wordsArray objectAtIndex:i];
NSLog(#"\n %#",[subString uppercaseString]);
}
}
return 0;
}
On input: "Test1 test2 test3"
I get: Test1
The code doesn't consider spaces. How do i scan the hall string?
A reason this isn't working is that scanf doesn't have a size of string to look for, so it is stopping at the first whitespace. Another method you can use is fgets() for the input.
This link here provides more information (I know it says c but you are using c functions here)
Allowing white spaces c

Add objects in a NSMutableArray declared in another function

Ideally, I would like to make a function add objects in a NSMutableArray, and then do whatever I want with this array in another function.
Here is what I've tried to do lately, of course it doesn't work but it gives you an idea of what I want to do:
- (void)someThing
{
(...)
NSMutableArray *arrayOfThings = [[NSMutableArray alloc] init];
while (theObject = [aNSEnumerator nextObject]) {
const char *theObject_fixed = [theObject UTF8String];
function_something(theObject_fixed);
}
// do something with arrayOfThings
}
void function_something(const char *file)
{
(...)
unsigned int *p = memmem(buffer, fileLen, bytes, 4);
NSMutableString *aString = [[NSMutableString alloc] initWithCapacity:48];
unsigned long off_to_string = 0x10 + 4 + ((void *)p) - ((void *)buffer);
for (unsigned long c = off_to_string; c<off_to_string+0x30; c++)
{
[aString appendFormat:#"%.2x", (int)buffer[c]];
}
NSLog(#"%s: %#", file, aString);
[arrayOfThings addObject:[aString copy]];
free(buffer);
There are two ways to go about this:
The first requires only a slight modification to your code will allow you to do what you want:
In the funciton someThing pass the mutable array as an additional parameter.
function_something(theObject_fixed, arrayOfThings);
Then change function_something to accept that parameter.
void function_something(const char *file, NSMutableArray *arrayOfThings) {
// Code remains the same
}
The other and in my opinion better solution would be for the function_something to return the fixed string as an NSString object and let someThing do the adding to the mutable array.
So we get something like this in someThing:
...
NSString *aString = function_something(theObject_fixed);
[arrayOfThings addObject:aString];
And then a redefined *function_something*:
NSString* function_something(const char *file) {
...
return [aString autorelease];
}
By the way, your code is leaking memory. Be careful with you retain/release/autorelease.

Objective-C Iterating through an NSString to get characters

I have this function:
void myFunc(NSString* data) {
NSMutableArray *instrs = [[NSMutableArray alloc] initWithCapacity:[data length]];
for (int i=0; i < [data length]; i++) {
unichar c = [data characterAtIndex:i];
[instrs addObject:c];
}
NSEnumerator *e = [instrs objectEnumerator];
id inst;
while (inst = [e nextObject]) {
NSLog("%i\n", inst);
}
}
I think it fails at [instrs addObject:c]. It's purpose is to iterate through the hexadecimal numbers of an NSString. What causes this code to fail?
A unichar is not an object; it's an integer type.
NSMutableArray can only hold objects.
If you really want to put it into an NSMutableArray, you could wrap the integer value in an NSNumber object: [instrs addObject:[NSNumber numberWithInt:c]];
But, what's the point of stuffing the values into an array in the first place? You know how to iterate through the string and get the characters, why put them into an array just to iterate through them again?
Also note that:
the "%i" NSLog format expects an integer; you can't pass it an object
for hexadecimal output, you want "%x", not "%i"
If the function is only meant to display the characters as hexadecimal values, you could use:
void myFunc(NSString* data)
{
NSUInteger len = [data length];
unichar *buffer = calloc(len, sizeof(unichar));
if (!buffer) return;
[data getCharacters:buffer range:NSMakeRange(0, len)];
for (NSUInteger i = 0; i < len; i++)
NSLog(#"%04x", (unsigned) buffer[i]);
free(buffer);
}
This is just a little bit more efficient than your approach (also, in your approach you never release the instrs array, so it will leak in a non-garbage-collected environment).
If the string contains hexadecimal numbers, then you will want to repeatedly use an NSScanner's scanHexInt: method until it returns NO.
void myFunc(NSString* data)
{
NSScanner *scanner = [[NSScanner alloc] initWithString:data];
unsigned number;
while ([scanner scanHexInt:&number])
NSLog(#"%u", number);
[scanner release];
}

Accept string values in NSArray from the user

hi i want to accept string values into the object of NSArray at run time from the user heres what i tried
-(void)fun
{
NSArray *arr = [[NSArray alloc]init];
for(int i =0;i<3;i++)
{
scanf("%s",&arr[i]);
}
printf("Print values\n");
for(int j =0; j<3;j++)
{
printf("\n%s",arr[j]);
}
}
i am getting an error can you please help me out regarding this and is their any alternative to scanf in objective c.
Thank you
scanf() with a %s format will read the string into a C array, not an NSArray object. You need to read the string into a C array, then make an NSString object to add to your NSArray. You also need to have a mutable array to make your code work. Example:
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:3];
for (int i = 0; i < 3; i++)
{
char buf[100];
scanf("%s", buf);
NSString *str = [NSString stringWithCString:buf encoding:NSASCIIStringEncoding];
[arr addObject:str];
}
You can use NSLog() to print your strings later on.
use NSMutableArray instead;
than you can use also
[arr addObject:tempVar];