How to get value of NSTextField dynamic add to NSMutableArray? - objective-c

I have a Combobox contains 30 items (1,2,...,30). I want to select item in Combobox, create dynamic NSTextField same item selected in Combobox. Then user input text to NSTextField, and then click on button to get all text of each NSTextField add to NSMutableArray.
I use bellow code to get text from NSTextField and add it to Array but it can only get from 1 NSTextField:
NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
[SSID_Arr addObject:ssidtxt.stringValue]; // get text from NSTextField
NSLog (#"SSID_Arr : %#",SSID_Arr);
NSString *strSSID;
for(int j=0; j < [SSID_Arr count]; j++)
{
strSSID = [NSString stringWithFormat:#"\r\nSSID : %#", [SSID_Arr objectAtIndex:j]];
}
Do you have suggestion? Thanks in advance

just make
NSMutableArray *SSID_Arr = [NSMutableArray new];

NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
gives an array with space reserved for x values.But there is no value in that position right now
So first fill up the array via a loop and then continue as
NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
for (int i=0; i<x; i++) {
[SSID_Arr addObject:ssidtxt.stringValue];
}
Now the array contains x values and you can proceed.Please note the one textfield value is getting populated here x times.If you have to store all textfield values ,write a loop suitable to achieve all values and add it

Related

Individual objects from Array are showing on NSLog with NSString but when addObject: to NSMutableArray, Array is null?

THIS IS WHERE THE PROBLEM LIES
NSString *individualEventTitle;
NSMutableArray *eventTitleArray = [[NSMutableArray alloc]init];
for (NSUInteger index =0; (index < 8) ; index++){
EventsList *eventList = [[EventsList alloc] initWithIndex:index];
NSString *individualEventTitle = eventList.eventTitle;
/*self.eventTitleArray = eventList.eventTitle;
self.eventLocationArray = eventList.eventLocation;
self.eventIconArray = eventList.eventIcon;
self.eventPriceArray = eventList.eventPrice;
self.eventTypeArray = eventList.eventType;*/
[eventTitleArray addObject:individualEventTitle];
NSLog(#"Title of Event:%#", individualEventTitle);
NSLog(#"Array of Event:%#", eventTitleArray);
}
So from my understanding, the loop should go through itself 8 times (to account for the number of items in my class array). To confirm this I NSLogged a string for the eventList.eventTitle and assigned it to a string called individualEventTitle. Upon reading the console I received ALL 8 event Titles.
However, when I add the command at the end of the loop, to add that string to the NSMutableArray *eventTitleArray. It appears null in the console when NSlogged.
I would highly appreciate some help, as I've spend the last two nights just tinkering with it with no avail!
Thank you!
Contents of my console
Console
Try to replace this line
[eventTitleArray addObject:[NSString stringWithFormat:#"%#",individualEventTitle]];

Filtering a NSMutableArray and returning only numbers

I have a NSMutableArray called myMutableArray inside him, I have the following values:
('R$ 118.98','AE 12.00 er','R$ 456.99')
What I would do, is find a way to filter the information contained within this array, thus making it returns only numeric characters, for example:
('118.98','12.00','456.99')
I have a simple code who get the lines inside an array:
for(int x=0; x<[myMutableArray count]; x++){
myMutableArray[x];//We need to find a way to filter and update this informations to only store numbers.
}
What the code I can put in my code to filter the information inside my array to only storing numbers?
Try:
//create a new mutable array to store the modified values
NSMutableArray *arrFinal = [[NSMutableArray alloc] init];
//fast-enumerate within myMutableArray
for (NSString *strCurrent in myMutableArray) {
NSString *strModified = [strCurrent stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
[arrFinal addObject:strModified];
}
NSLog(#"%#",[arrFinal description]);

How do I insert strings into an NSArray and return them later for printing or comparing?

I am trying to create a NSMutableArray of size 100 and then fill every index with the a string of three blank spaces, " ". I am either creating the array incorrectly or I am returning the objects incorrectly. Here is my method that creates the array:
-(void) initBoard{
_board = [board initWithCapacity:100];
for(int i =0; i < 100; i++){
[_board addObject:#" "];
}
}
This method is not functioning properly. I added an a for loop after the one above that contained NSLog #"#%", [_board objectAtIndex: i]; so that I could verify that there were actually strings comprised of three blank spaces at every index, but all that it would print out in the console was "nil". So far I have tried:
I thought that maybe the addObjectAtIndex was the problem so I just changed it to addObject and changed the initWithCapacity to just init]; to allow the for loop to build the array to size 100.
Then I thought that maybe the problem was with trying to insert the literal string, #" " (IDK how to make Stackoverflow show my 3 spaces, not just 1), so I added NSString *input = #" "; right before the for loop, and then changed [_board addObject:#" "]; to `[_board addObject:input;
Any suggestions on how to actually create an NSMutableArray of size 100 and make every spot contain the same string of three blank spaces?
You need to allocate and than initialize the mutable array:
-(void) initBoard{
_board = [[NSMutableArray alloc] initWithCapacity:100];
for(int i =0; i < 100; i++){
[_board addObject:#" "];
}
}

Accesing objects in multidimensional array, Obj-c

I have an array holding other arrays, but cant get this to work!! IM trying to pull string values from subArrays I can access first the first subarray no problem but then when i try changing the label to the object in second array my prog crashes anyidea on how i should approach this?
int count = 0; // variable to access the required sub array
NSArray* myArray; // array holding other arrays
UILabel* mylabel; // label to display my string values from the array s
-(void) setLabel
{
NSArray* subArray = [myArray objectAtIndex: count];
[myLabel setText:[subArray objectAtIndex:1]]; // this works fine
}
-(void) changeLabelToNextArray
{
count ++
[self setLabel]; //program crashes here when try to load label from next array
}
Why are you doing this?
[myLabel setText:[subArray objectAtIndex:1]];
This will crash if
subArray does not have an object at index 1
the object at index 1 cannot be set as the label text (i.e cannot be made a string)
I think some more information on how your arrays are structured would help give a better answer to what the problem is.
EDIT (Based on comments below)
Try this:
NSArray* myArray; // Contains 5 subarrays, each containing 5 strings
UILabel* myLabel1;
UILabel* myLabel2;
UILabel* myLabel3;
UILabel* myLabel4;
UILabel* myLabel5;
int count = 0; // Keeps track of which subarray we are on
- (void)setLabel
{
NSArray* subArray = [myArray objectAtIndex:count];
[myLabel1 setText: [subArray objectAtIndex:0]]
[myLabel2 setText: [subArray objectAtIndex:1]]
[myLabel3 setText: [subArray objectAtIndex:2]]
[myLabel4 setText: [subArray objectAtIndex:3]]
[myLabel5 setText: [subArray objectAtIndex:4]]
count = (count + 1) % 5; // Ensures that count is always 0 to 4
}
Now whenever you call setLabel, the text should change on all 5 of your labels provided you do indeed have 5 strings in each of the 5 subarrays.
Maybe you can try to use C array :
id myArray[iMax][jMax];
subArray[i][j] = myArray[a][b];

Add cell to an Array using data from a UIPickerView?

I have a UIPickerView with three components, and each component has NSIntegerMax for the numbers. So, I just need to get the data from each component, and send it to another ViewController called, CreationViewController. The three objects I need to send are strings, set up like so:
NSInteger supplyData = [supplypick selectedRowInComponent:0];
NSInteger mineralData = [supplypick selectedRowInComponent:1];
NSInteger vespeneData = [supplypick selectedRowInComponent:2];
So, I would like to add each cell in the format of this log:
NSLog(#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData);
All I need to know it what to put...
NSMutableArray * array = [[NSMutableArray alloc] init];
[array addObject: ];
^^HERE^^
This is all in one function. Please help! Thanks!
[array addObject:[NSString stringWithFormat:#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData]];