UITextField number values into NSMutableArray - ios

I want to create a program with user able to type in float number into the UITextField and store it into array. Can someone kindly provide me the coding or guide?

Firstly take a class level array and initialize it in viewDidLoad and then add float value on any button action like this.....
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:5];// define your capacity over here.
-(IBAction)addValue
{
[arr addObject:[NSNumber numberWithFloat:[yourTxtFld.text floatValue]]];
}

Related

How to create a variable sized float type array in objective c?

I need a float type array; don't know the size of array initially. When I will get the value of size, I want to add some float values in it by a for loop. This array need to be global because I want to update it from another view controller class. How can I do this?
For this I declared a NSMutableArray type object and add float value by a for loop.
#property (nonatomic, strong) NSMutableArray *speedRate;
for (NSInteger i = 0; i < self.assetArray.count; i++){
[self.speedRate addObject:[NSNumber numberWithFloat:1.0f]];
}
NSLog(#"speedRate.count =%lu",(unsigned long)self.speedRate.count);
NSLog(#"value =%f",[[self.speedRate objectAtIndex:1]floatValue]);
But I got speedRate.count =0 and value =0.000 . I want speedRate.count will be same as self.assetArray.count and value =1.0 . How can I achieve this?
Your code never actually allocates the array. Add
self.speedRate = [[NSMutableArray alloc] init];
before the loop.

update value at specific index in NSArray

I am hoping not to need to use an NSMutableArray here. I have an array with 10 elements. I want to change the value at index 4. Is there a way to do this without having to use NSMutableArray? The problem with NSMutableArray is that we can change anything about it, including its size. I don't want the size of this array to change accidentally. I just want to change the value at index 4 from say 22 to 25. How might I do that? doing array[4]=25 is not working.
NSArray *ar1 = #[#"1",#"2"];
NSMutableArray *ar1update = [ar1 mutableCopy];
ar1update[1] = #"Changed";
ar1 = [NSArray arrayWithArray:ar1update];
The only way is to create a new NSArray and change your pointer to a new NSArray. I can give an example...
In interface:
#property (strong, nonatomic) NSArray *myArray;
In implementation:
- (void) updateMyArray{
NSMutableArray *myArrayMut = [self.myArray mutableCopy];
myArrayMut[4] = #"new item";
self.myArray = [myArrayMut copy];
}
So basically, you can create a mutable copy temporarily, make the change you need, and then make an immutable copy. Once you have the immutable copy, you can point myArray to the new copy. As long as you are only changing existing items in updateMyArray and the myArray starts out with 10 items or less, you will never be able to have more than 10 items.
If you don't wish to use NSMutableArray how about a plain old C array? E.g.:
int array[10];
...
array[4] = 25;
You can store Objective-C objects in such an array and ARC will handle the memory management.
If you really want a fixed-sized NSArray/NSMutableArray you can do that by subclassing those types yourself - subclassing NSArray only requires implementing two methods and you can use an underlying C array or NSMutableArray for the actual storage.
HTH

NSMutableArray Allocate then replaceObjectAtIndex

I have a NSMutableArray that i define in the header file as:
#property (strong, nonatomic) NSMutableArray *tempPhotosArray;
Then i allocate as:
_tempPhotosArray = [[NSMutableArray alloc] init];
What i'd like to know is if i then go to replaceObjectAtIndex the program will complain on an out of bounds. I want to keep only a set number of items in that array, so is it possible to do a insert or replace? i.e. if at index 0 it is empty do an insert, if there is an object already replace it?
Thanks
i think i agree with Hani Ibrahim. Since you said you only want to keep a set number of objects in the array. So how many you want?
// add these code when you initialize the array
int aSetNumber = 5;
_tempPhotosArray = [[NSMutableArray alloc] init];
for (int i = 0; i < aSetNumber; i++)
{
[_tempPhotosArray addobject: [NSNull null]];
}
i guess then you can do whatever you want, i don't know what exactly you want to do in this case, but i would check if the object in that position is NSNUll, if so, replace that, if not, i don't know what you want them
//use these code when you trying to insert the real object
if([[_tempPhotoArray objectAtIndex:anIndex] isKindOfClass: [NSNull class]])
{
//replace it here
}
As to why you are getting an error, what everyone else wrote is accurate, but....
The description of what you want doesn't match what an NSArray is. It sounds like you want a list of up to 5 items and never more than 5. It might be that if you try to add a 6th item the "oldest" goes away. Like a "recently opened" file history. You can make this type of functionality with an NSArray, but that's not what it is out of the box.
I would suggest making your own object class. I'm not going to write all the code for you, because this sounds suspiciously like programming homework, but I will point you in the correct direction.
FivePack <-- our class
NSArray *storage; <-- where we house the data
// a public method which lets you add things.
- (void)addItem:(id)item {
int indexOfLastItemInArrayToSave = 4;
if (storage.length < 4)
indexOfLastItemInArrayToSave = length-1;
NSRange range = NSMakeRange(0, indexOfLastItemInArrayToSave);
NSArray *temp = [storage subArrayWithRange:range];
// now create a new array with the first item being "item" that
// was passed in and the rest of the array being the contents of temp.
// Then save that to storage.
}
What you want to do with the data and writing something to get it from your new object is up to you, because I'm not sure how you want to do it.
There are no objects in the array when you initially created it, so there is nothing to replace.
Like this?
if([_tempPhotosArray count] > 0)
//replace object
else
//add object to array

Unable to add text from UITextField to an Array

Currently I have a custom view table cell and a text field just above it. I want to get the text from the UItextfield and put that into an NSMutableArray.
Pseudocode:
String text = _textfield.text;
[array addObject:text];
NSLog{array}
In my header file I have created the textfield and the array.
I currently receive the error : 'CustomTableView:[340:11303] array: (null)' when I NSLog.
I am not to sure why the text from the textfield is not getting added to the array. If any one is able to help it will be greatly appreciated.
Note - My textfield is above the custom cell not in it. I have even tried just adding a string to the array directly and logging it and I get the same error. So I would assume that this is something to do with the array.
did you initialize your Array.take a MutableArray and initialize it.
NSMutableArray *array=[NSMutableArray alloc]init];
You mentioned that you have declared the textfield and the array in your header file...
Have you initialised the variable array?
e.g.
array = [NSMutableArray new];
It looks like you are not actually creating the array. In Objective C, you do not create things in header file, you declare them. The implementation files(.m files) do all the work.
Try this:
NSString *text = _textfield.text;
array = #[text]
NSLog( #"%#", array );
This is how you should print your array,
NSLog(#"%#", array);
It looks as if your a newbie to ios.Go through the objective-c and Read the apple documentation carefully.
NSString * text = self.textfield.text;
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:text];
NSLog(#"%#",array);
For me this is what worked...
I have taken one textfield inside tableviewcell. I am creating textfields based on dynamic data. My requirement is , I need to get textfields text which are created dynamically.
For getting text in another method
NSIndexPath *indexPath = [tableViewObj indexPathForCell:customCell];
if (indexPath.row==0)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==1)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==2)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
Like this I have added textfield text to array. Let me know if you have any doubts.

NSMutableArray property initialization and updating

Suppose I have a #property that is an NSMutablearray that is to contain scores used by four objects. They will be initialized as zero and then updated during viewDidLoad and throughout operation of the app.
For some reason, I can't wrap my mind around what needs to be done, particularly at the declaration and initialization steps.
I believe this can be a private property.
#property (strong, nonatomic) NSMutableArray *scores;
#synthesize scores = _scores;
Then in viewDidLoad I try something like this but get an error. I just need help with syntax, I think. Or I'm missing something very basic.
self.scores = [[NSMutableArray alloc] initWithObjects:#0,#0,#0,#0,nil];
Is that an appropriate way to initialize it? Then how do I add (NSNumber *)updateValue to, say, the nth value?
Edit: I think I figured it out.
-(void)updateScoreForBase:(int)baseIndex byIncrement:(int)scoreAdjustmentAmount
{
int previousValue = [[self.scores objectAtIndex:baseIndex] intValue];
int updatedValue = previousValue + scoreAdjustmentAmount;
[_scores replaceObjectAtIndex:baseIndex withObject:[NSNumber numberWithInt:updatedValue]];
}
Is there a better way of doing this?
You are initializing in viewDidLoad, However you should do it in init.
These both are similar, and perfectly valid.
_scores = [[NSMutableArray alloc] initWithObjects:#0,#0,#0,#0,nil];
or,
self.scores=[[NSMutableArray alloc]initWithObjects:#0,#0,#0, nil];
Your last question... Then how do I add (NSNumber *)updateValue to, say, the nth value?
If you addObject: it will be added at last. You need to insertObject:atIndex: in your required index, and all following objects will shift to next indices.
NSInteger nthValue=12;
[_scores insertObject:updateValue atIndex:nthValue];
EDIT:
After your edit,
NSInteger previousValue = [[_scores objectAtIndex:baseIndex] integerValue];
NSInteger updatedValue = previousValue + scoreAdjustmentAmount;
[_scores replaceObjectAtIndex:baseIndex withObject:[NSNumber numberWithInt:updatedValue]];

Resources