Adding successive words to array causes error - ios

I am trying to create an array of values that when printed, will look like the following:
{
headerTitle = "#";
rowValues = (
""
);
} {
headerTitle = A;
rowValues = (
"Abaco Barb",
Abenauer,
"Australian Stockhorse",
);
} {
headerTitle = B;
etc…
The program fails with a "doesNotRecognizeSelector:" error on the statement indicated below.
The array is defined as:
NSMutableArray *words = [[NSMutableArray alloc]init];
THE RELEVANT CODE:
else
{
letterString = [[dataArray objectAtIndex:i] substringToIndex:1];
NSLog(#"lastLetter = %#",lastLetter);
NSLog(#"letterString = %#",letterString);
if ([lastLetter isEqualToString: letterString])
{
NSLog(#"before words, dataArray %#", [dataArray objectAtIndex:i]);
//(FAILS on the next instruction on adding [dataArray objectAtIndex:i] =
// Abenauer)
[words addObject:[dataArray objectAtIndex:i]];
NSLog(#"words - %#",words);
NSLog(#"after words");
}
else
{
NSMutableDictionary *row = [[NSMutableDictionary alloc] init];
[row setValue:lastLetter forKey: #"headerTitle"];
[row setValue: words forKey:#"rowValues"];
[content addObject:row];
NSLog(#"content - %#",content);
lastLetter = letterString;
words =[dataArray objectAtIndex:i];
NSLog(#"words - %#",words);
}
}
OUTPUT created before failure:
2014-03-21 11:37:42.967 EquineDiary[1864:a0b] lastLetter = #
2014-03-21 11:37:42.967 EquineDiary[1864:a0b] letterString = A
2014-03-21 11:37:42.968 EquineDiary[1864:a0b] content - (
{
headerTitle = "#";
rowValues = (
);
}
)
2014-03-21 11:37:54.434 EquineDiary[1864:a0b] words - Abaco Barb
2014-03-21 11:37:54.435 EquineDiary[1864:a0b] lastLetter = A
2014-03-21 11:37:54.436 EquineDiary[1864:a0b] letterString = A
2014-03-21 11:37:54.436 EquineDiary[1864:a0b] before words, dataArray Abtenauer
Thanking you in advance for your help.
NOTE 1:
The entire error is "CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:]:" Words is defined at the beginning of the Method, outside of the for loop. "Abaco Barb" is already in words when I am trying to add "Abtenauer" which is in [dataArray objectAtIndex:i].
All I am trying to do is put all names that begin with "A" into an array (words) until the word no longer begins with A at which time I write the words array to a row with the key "row values". A is written to the row with a key "header titles".
NOTE 2:
I have discovered the issue but don't know how to fix it. After I write the data to row I have the code " words =[dataArray objectAtIndex:i];" for the purpose of wiping the previous values (i.e. "A" values) so that I can start adding words which begin with the next letter (i.e. "B"). It doesn't like adding objects to words after I do this. Why is this? And what is the best way to wipe the values to begin again?

If you want a new array just create a new array.
words = [[NSMutableArray alloc] init]

It looks like Abenauer is not a string. It does not have quotes around it.

Related

How to compare two nsarrays with different values?

Hi i have two nsarrays.
Array A:
arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
Array B:
arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
I need to compare two arrays and get the value from array A by comparing with array B having value as selected. That is i should get german,chinese and russian sepearted by comma as nsstring.
Try this:
NSMutableArray *arrSelected = [[NSMutableArray alloc] init];
NSArray *arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
NSArray *arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
for(int i = 0; i< arrProductSelectionB.count-1;i ++) {
if ([arrProductSelectionB[i] isEqualToString:#"selected"]) {
[arrSelected addObject:arrProductSelection[i]];
}
}
NSString *strSelected = [arrSelected componentsJoinedByString:#","];
NSLog(#"%#", strSelected);//output: German,Russian,Chinese
If you can make this type of array then manage easly.
(
{
flag = deselcted;
name = English;
},
{
flag = selected;
name = German;
},
{
flag = deselcted;
name = Russian;
},
{
flag = deselcted;
name = Chinese;
}
)
==> Array[(dictionary),(dictionary),.....]
if you need result in an array, Here is the function:
NSMutableArray*resultArray=[[NSMutableArray alloc]init];
for(int i=0; i<arrProductSelectionB.count;i++){
if ([[arrProductSelectionB objectAtIndex:i]isEqualToString:#"selected"]) {
[resultArray addObject:[arrProductSelection objectAtIndex:i]];
}
}
resultArrayhave the values which you need.
first i would make sure both arrays have same counters for safety something like
if (arrProductSelection.count == arrProductSelectionB) {
//than all you need is one for cycle something like :
for (int i = 0; i < arrProductionSelectionB.count; i++) {
if ([arrProductionSelectionB[i] isEqualToString: #"selected"]) {
do something magically with arrProductSelection[i];
}
}
}

Creating Different Permutations from different Arrays Values

I am trying to write a method which allows me to create a different String permutations, based on the values I have stored within 3 different arrays. Currently I have a NSMutableDictionary that contains 3 different keys, and associated to each key we have an NSMutableArray array that contains a list of different String objects.
How woulld I loop through each of my NSMutableArray arrays which are stored within my NSMutableDictionary, and build a string value for each node from the arrays. So essentially, something like the following: A1[0] = "Hello", A2[0] = "There", A3[0] = "Guys", which would essentially build me a string like this: "HelloThereGuys".
Any suggestions, or possibly different approaches to this problem will be appreciated.
First of all I want to agree with hochl. ;-) But I think, that you want to create a string containing a word from each array in the same index position?
NSMutableArray *enums = [NSMutableArray new];
for( NSString *key in dictionary )
{
NSEnumerator *enum = [dictionary[key] objectEnumerator]; // Forgot that in prior version
[enums addObject:enum];
}
while( YES )
{
NSMutableString *line = [NSMutableString new];
BOOL done = NO;
for( NSEnumerator *enum in enums )
{
NString *word = [enum nextObject];
if( word == nil )
{
done = YES;
break;
}
[line addString:word];
}
if (done)
{
break;
}
NSLog (#"%#", line );
}
Did I get you right?

ios sort a mutable dictionary order [duplicate]

This question already has answers here:
NSDictionary with ordered keys
(9 answers)
Closed 8 years ago.
I saw many examples on SO but I'm not sure if it applies to this situation. Everywhere it says NSMutableDictionries are not guaranteed an order...but I'm getting my data from the server...so my NSMuteDic looks like this:
{
joe = (
{
fromName = joe;
id = 25;
theMessage = "this is going to be a really big message...";
timeAdded = "2014-04-07 21:08:12";
toName = "me";
},
{
fromName = joe;
id = 10;
theMessage = "why???";
timeAdded = "2014-04-05 20:10:04";
toName = "me";
}
);
bob = (
{
fromName = "me";
id = 24;
theMessage = "blah blah";
timeAdded = "2014-04-06 21:15:06";
toName = bob;
},
{
fromName = bob;
id = 22;
theMessage = message;
timeAdded = "2014-04-06 20:11:57";
toName = "me";
}
);
//more entries here
}
What I want to do is change the order...put bob first and joe second. Is this really impossible to do? I saw many very complex solutions...there's no easy way to do this with just a for loop?
cellForRowAtIndexPath:
NSMutableArray *temp = [myDict objectForKey:keys[indexPath.row]];
cell.Message.text = [[reverse lastObject] valueForKey:#"theMessage"];
cell.dateTime.text = [[reverse lastObject] valueForKey:#"timeAdded"];
return cell;
This is how I'm using it...and when a row is selected I pass the array to the next view controller. The reason why I want to reorder is if a new message will be inserted in the pushed view controller, I want that dictionary to be first in the list so the root view controller can be reordered.
NSArray *keys = [myDict allKeys];
[myDict removeObjectForKey:to];
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:[myDict.count];
temp = myDict;
[myDict removeAllObjects];
[myDict setObject:currentDict forKey:to];
for (int i=0; i<temp.count; i++) {
[myDict setObject:[temp valueForKey:keys[i]] forKey:keys[i]];
}
That's not working because it looks like since myDict is a NSObject, temp gets changed every time myDict changes...from the looks of it the logic should work but it isn't...
NSMutableDictionary is not ordered. There is nothing you can do to guarantee the order of keys because NSDictionary makes no attempt to preserve any particular ordering. To go over a dictionary in a particular order, you have to make an array of the keys that is in the order you want, then iterate that and fetch the corresponding values.
// Get the keys
NSArray *keys = [myDict allKeys];
// Sort the keys
NSArray *sortedArray = [NSArray arrayWithArray:[keys sortedArrayUsingComparator:^(NSString* a, NSString* b) {
return [a compare:b];
}]];
// Iterate the dictionary
for (NSUInteger n = 0 ; < [sortedArray count]; n++) {
id value = [myDict objectForKey:[sortedArray objectAtIndex:n]];
}

UITextfield text in array will not change a label's text in another array

Please help. What am I doing wrong? textfield.text goes into one array and is suppose to change a label that belongs to another array.
I have multiple textfields. Im trying to save the text of each field to an array and then setAlpha to 0. Then I have an equal amount of labels that I want to change to the textfield's text. I have tried using mutable and immutable arrays. I keep getting errors.
I've tried multiple ways and its got to be something simply dumb I'm missing. I've greatly reduced these arrays just to post here.
_namesText = [NSMutableArray arrayWithObjects:_nameLabel1.text, _nameLabel2.text, nil];
_names = [NSMutableArray arrayWithObjects:_nameLabel1, _nameLabel2, nil];
_nameInputs = [NSMutableArray arrayWithObjects:_p1NameTextField, _p2NameTextField, nil];
_playerNameText = [NSArray arrayWithObjects:_p1NameTextField.text, _p2NameTextField.text, nil];
enter code here
- (IBAction)enterNamesButton:(id)sender {
//These don't work.
for (int i = 0; i < _numberOfPlayers; i++) {
[_names[i] setAlpha:1];
NSString *tempString = [[NSString alloc]initWithString:[_nameInputs[i] text]];
[_lastTry addObject:tempString];
}
//Then tried this. This is after trying for 2.5 hours and different coding.
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
UILabel *cell = [[UILabel alloc] init];
cell.text = _lastTry[i];
//[[_namesText[i] textLabel] text] = #"Idiot";
_namesText[i] = cell;
NSLog(#"%#", _namesText[i]);
// This works (but bad practice) and I want to loop through arrays that each UI element belongs to instead of typing it all out.
// _nameLabel1.text = _p1NameTextField.text;
// _nameLabel2.text = _p2NameTextField.text;
I expect this to work but NOPE!!!!
Assuming the following:
Array _fromTextField is the textfield you want to copy from and set Alpha to 0
Array _toLabelField is the label you want the text to appear in
Array _toStoreStrings is where you keep all the strings, for later maybe ?
NSMutableArray *_fromTextField = [[NSMutableArray alloc] init]; // Put the Text Fields in Here
NSMutableArray *_toLabelField = [[NSMutableArray alloc] init]; // Put the Labels in Here
NSMutableArray *_toStoreStrings = [[NSMutableArray alloc] init];
int count = 0;
int overRun = [_toLabelField count];
for (UITextField *tf in _fromTextField)
{
tf.alpha = 0;
NSString *tempString = [[NSString alloc]initWithString:tf.text];
[_toStoreStrings addObject:tempString];
if (count < overRun) // Check just in case Array sizes not same
{
UILabel *lb = [_toLabelField ObjectAtIndex:count];
lb.text = tempString;
}
else
break;
count++;
}
The issue your code faces is you have an array of NSObject. The compiler has no idea they are UITextField or UILabel unless you cast them with (type_cast *) or point a new typed variable at them. Hence the crashes you were seeing.
I would do it this way:
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
}
I cannot see why this won't work... does it?

Issue with UITextfield into a NSMutableArray

I had this working using NSArray until I realize I need to insert a string into array. Now I'm changing this to a NSMutableArray, but having issues with my syntax. Am I allowed to use componentsJoinedByString in NSMutable arrays?
NSString *item;
int numofSeg;
int needSeg;
if (textfield.text == #"::") {
numofSeg = 0;
}
NSMutableArray *dlist = [[NSMutableArray alloc]init];
//take string from textfield and split it into a list on colons.
dlist = [textfield.text componentsSeparatedByString:#":"];
//run through list to count how many segments. Non blank ones.
for (item in dlist) {
if (item != #""){
numofSeg = numofSeg + 1;
NSLog(#"%#",numofSeg);
}
}
//determine number of segments
needSeg = 8 - numofSeg;
while (needSeg > 0) {
//insert 0000 at blank spot
[dlist insertString:#"0000" atIndex:#""];
needSeg = needSeg - 1;
}
for (item in dlist) {
if (item == #"") {
//remove blank spaces from list
[dlist removeAllObjects:item];
}
}
//join the list of times into a string with colon
NSString *joinstring = [dlist componentsJoinedByString:#":"];
NSLog(#"%#",joinstring);
I don't think NSMutableArray has a method called insertString:atIndex:. You should probably use insertObject:atIndex: instead.
What error do you get exactly?
Edit:
If you only use the NSMutableArray to insert #"0000" before #" ", then you can simply do:
string = [string stringByReplacingOccurrencesOfString:#" " withString:#" 0000"];
or something like that. No need to use NSMutableArray.
I finally figured it out. This is what I ended up using to fix this:
NSString *z = #"0000";
z = [z stringByAppendingString:#""];

Resources