Works with number of string contained in NSArray - objective-c

I need to pick string valur from an NSMutableArray then save it into a plist. I've builded an NSMutableArray to display infos in table View. Maximum allowed index is 8. (paste just two in example)
The problem if the String doesn't exist, I get the following error:
sDict is a dictionary for saving datas to a property list file.
the code:
- (IBAction)save:(id)sender {
(...)
NSString *One;
NSString *Two;
...etc
if ([self.smOne objectAtIndex:0])
One = [self.smOne objectAtIndex:0];
if ([self.smOne objectAtIndex:1])
Two = [self.smOne objectAtIndex:1];
...etc
if (One)
[sDict setObject:[self.smTwo objectAtIndex:0]
forKey:[UserM stringByAppendingString:One]];
[sDict setObject:[self.smThree objectAtIndex:0]
forKey:[UserS stringByAppendingString:One]];
[sDict setObject:[self.smFour objectAtIndex:0]
forKey:[UserP stringByAppendingString:One]];
if (Two)
[sDict setObject:[self.smTwo objectAtIndex:1]
forKey:[UserM stringByAppendingString:Two]];
[sDict setObject:[self.smThree objectAtIndex:1]
forKey:[UserS stringByAppendingString:Two]];
[sDict setObject:[self.smFour objectAtIndex:1]
forKey:[UserParM stringByAppendingString:Two]];
...etc
}
This code works if all objects are present, but fails if it miss one of the object at index.
I really don't know how to check properly if the object is present or not, cause code above seem's to don't works well.
I've tried with [self.smOne count] but as problem to pass as a Int or String to make conditions with.
Thanks for answer.

it looks like you're explicitly checking smOne from indices 1 through 8. But you also mentioned that the array can have up to 8. So if it's missing, say, 6, 7 and 8, you'd still be calling [smOne objectAtIndex:6], which would result in an NSRangeException being raised as 6 is out of bounds for the array.
try this instead:
int i = 0;
for ( NSString *aString in self.smOne )
{
[sDict setObject:[self.smTwo objectAtIndex:i]
forKey:[UserM stringByAppendingSting:aString]];
[sDict setObject:[self.smThree objectAtIndex:i]
forKey:[UserS stringByAppendingString:aString]];
[sDict setObject:[self.smFour objectAtIndex:i]
forKey:[UserP stringByAppendingString:aString]];
i++;
}
it'll go through each object in the smOne array and add the object into sDict regardless of how many items you have in smOne.
also, be careful with how you're generating your keys. there's the possibility that [UserM stringByAppendingSting:aString] won't always be unique.

Sorry to ask again but i have dificulties to find how to rebuild arrays from the key/string couple saved with the loop.
i've tried this:
int i = 0;
for (NSString *SMServ in [temp objectForKey:
[UserMen stringByAppendingFormat:#"%d",i]]){
NSString *SMMem[i];
SMMem[i] = [temp objectForKey:[UserMen stringByAppendingFormat:#"%d",i]];
NSArray *theArray = [ NSArray arrayWithObjects:SMMem count:i];
i++;
}
But nothing happens.
if i try with for (i = 0; i < 9; i ++) and (i + 1) instead of [i], i get same bounds errors in the first example.
thanks again for help.

Well, finally it ,works. Thanks for link to the documentation, i read too fast last time.
I'm sure this is not the cleanest way, but it works with this loop:
for (key in sDict) {
if ([sDict valueForKey:[UserMen stringByAppendingFormat:#"%d",i]]) {
tempMe = [sDict valueForKey:[UserMen stringByAppendingFormat:#"%d",i]];
if (tempMe) {
[manArray insertObject:tempMe atIndex:(0 + a)];
[bankArray insertObject:[NSString stringWithFormat:#"%d",i] atIndex:(0 + a)];
a++;
}
}
if ([sDict valueForKey:[UserSerial stringByAppendingFormat:#"%d",i]]) {
SMSer = [sDict valueForKey:[UserSerial stringByAppendingFormat:#"%d",i]];
if (SMSer) {
[serArray insertObject:SMSer atIndex:(0 + b)];
b++;
}
}
if ([sDict valueForKey:[UserPart stringByAppendingFormat:#"%d",i]]) {
SMPart = [sDict valueForKey:[UserPart stringByAppendingFormat:#"%d",i]];
if (SMPart) {
[partArray insertObject:SMPart atIndex:(0 + c)];
c++;
}
}
i++;
}

Related

Search String in NSDictionary store in NSMutableArray

I am trying to search a String in NSDictionary stored in NSMutableArray
for (int k = 0; k < [onlyActiveArr count]; k++) {
NSString *localID = [onlyActiveArr objectAtIndex:k];
NSLog(#"%#",localID);
int localIndex = [onlyActiveArr indexOfObject:localActiveCallID];
NSLog(#"%d",localIndex);
for (NSDictionary *dict in self.SummaryArr) {
NSLog(#"%#",[dict objectForKey:#"ActiveID"]);
if (![[dict objectForKey:#"ActiveID"] isEqualToString:localID]) {
NSLog(#"found such a key, e.g. %#",localID);
}
}
}
But I am getting
NSLog(#"found such a key, e.g. %#",localActiveCallID);
when the ID is still there in SummaryArr, I am checking if localID retrieved from onlyActiveArr is not present in dictionary.
Please suggest me how to overcome my problem.
You cannot make a decision that a key is not present until you finish processing the entire dictionary. Make a boolean variable initially set to NO, and change it to YES if you find an item in the dictionary, like this:
BOOL found = NO;
for (NSDictionary *dict in self.SummaryArr) {
NSLog(#"%#",[dict objectForKey:#"ActiveID"]);
found = [[dict objectForKey:#"ActiveID"] isEqualToString:localID];
if (found) break;
}
if (!found) {
NSLog(#"found such a key, e.g. %#",localID);
}
If you like predicates, then you can use the fact that accessing an inexistent key in a dictionary produces a nil value and make a predicate that filters out those dictionaries that have nil for your key.
If the count of the result is larger than zero, your key is somewhere in the array. It won't tell you where, though.
A snippet to show the idea:
NSPredicate *keyPred = [NSPredicate predicateWithFormat: #"NOT ActiveID == nil"];
BOOL found = [[self.SummaryArr filteredArrayUsingPredicate: keyPred] count] > 0;
I don't know how the performance stacks up, so if your data set is large you may want to check that the execution time is within your limits.

iOS adding object to an array from another array base on 3rd array value

EDIT: All array used in my project are NSMutableArray class
An overview of what I want to do is from my selectClueView, user can select a number from 3-10. Which represent the number of clue they will play. It will then generate list of random number between 0 and the objectArray.count and add the NSNumber into another array known as dataArray. Everything is working fine including prepareForSegue which transfer SelectClueViewController.dataArray to GamePageViewController.clueToSelect
However, I am stuck with loading data into the new array ds, from an array that hold all the object allDataObject. I am fairly new to iOS and because I had a working function in c#, I tried to replicate it in objective-C, unfortunately it seems that I can't replicate it fully.
In short, I'm trying to add data from allDataObject array into ds array with NSNumber values from cluesToSelect array.
Below are the coding which are used. Any help to fix the issue would be much appreciated. If there are any more information that I should give, please let me know.
SelectClueViewController.m
- (IBAction)onGoPress:(id)sender {
[self chooseNumber];
NSLog(#"Array got %d numbers",dataArray.count);
}
-(void)chooseNumber
{
[dataArray removeAllObjects];
maxCount = [numberOfClues.text intValue];
int count = 0;
do {
NSInteger rdmNumber = arc4random()%objectArray.count;
if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]])
{
NSNumber* number = [NSNumber numberWithInt:rdmNumber];
[dataArray addObject:number];
count++;
NSLog(#"random no - %d",rdmNumber);
}
} while (count < maxCount);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"sendNumber"]) {
GamePageViewController *gpViewController = [segue destinationViewController];
gpViewController.cluesToSelect = self.dataArray;
NSLog(#"Success");
}
}
GamePageViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
daoDS = [[ClueDataDAO alloc]init];
self.allDataObject = daoDS.PopulateDataSource;
NSLog(#"%d",cluesToSelect.count);
[self fillDataSample];
//for keyboard
self.answer.delegate = self;
}
-(void)fillDataSample
{
int count = 0;
do {
// [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];
ds = [[NSMutableArray alloc]init];
currentClueData = [[ClueData alloc]init];
int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
currentClueData = [allDataObject objectAtIndex:firstIndex];
[ds addObject:currentClueData];
count++;
} while (count < cluesToSelect.count);
NSLog(#"ds got %d object",ds.count);
}
EDIT:
I am now able to make it add in object into the ds array, unfortunately it only add once. Can someone look at my fillDataSample function?
In the line you point at at the beginning :
[self.ds addObject:[allDataObject objectAtIndex:[cluesToSelect objectAtIndex:count]]];
Just a wild guess, but [cluesToSelect objectAtIndex:count] returns an object. You're trying to pass that to another objectAtIndex:, which takes an int as argument. I'm guessing the error could come from that. You could try using .intValue if it's a number.
Edit :
Here's something more readable, considering cluesToSelect and allDataObject contain only NSNumbers :
int firstIndex = [[cluesToSelect objectAtIndex:count] intValue];
int secondIndex = [[allDataObject objectAtIndex: firstIndex] intValue];
[self.ds addObject:secondIndex];
As per our discussion,
Kindly check this one:
[self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count] intValue]]];
will do your work.
The reason as to why it didn't work for my new method of loading object from allClueData to a class object currentClueData to ds was because I did not nil and alloc init the class object after adding it to ds. The object were unable to overwrite their own value like they do in other language. (which was probably why I'm wrecking my brain for doing it in objective C) But after adding in nil the object and casting alloc and init, its working great now. thanks all :)
-(void)fillDataSample
{
int count = 0;
currentClueData = [[ClueData alloc]init];
ds = [[NSMutableArray alloc]init];
do {
// [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];
int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
currentClueData = [allDataObject objectAtIndex:firstIndex];
[ds addObject:currentClueData];
currentClueData =nil;
currentClueData = [[ClueData alloc]init];
count++;
} while (count < cluesToSelect.count);
NSLog(#"ds got %d object",ds.count);
}

Copying an object from one NSMutable array to another

I have done a bit of searching and not really found an answer to my question.
The code below is trying to copy the contents of a NSMutable array that contains some objects.
I have tried the code below however when run there is no error, but the new arrays do not get the objects as I would have thought they would.
csvContent is a array that contains objects from parsing a CSV file and the other NSMutable arrays round1, round2 etc have been defined in the header file.
The NSLOG output is correct but the arrays contain no objects.
for(int loopNumber = 0; loopNumber < [csvContent count]; loopNumber++)
{
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:#"1"])
{
[round1 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(#"round 1");
}
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:#"2"])
{
[round2 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(#"round 2");
}
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:#"3"])
{
[round3 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(#"round 3");
}
}
Did you actually create and initialize the arrays for round1, round2, and round3? In other words, make sure they are not nil when this loop is run.
Also, your code is terribly inefficient. You call [csvContent objectAtIndex:loopNumber] six times inside the loop. Try this (I'm using i instead of loopNumber to save typing):
for (int i = 0; i < csvContent.count; i++) {
NSArray *loopContent = csvContent[i];
NSString *val = loopContent[1];
if ([val isEqualToString:#"1"]) {
[round1 addObject:loopObject];
} else if ([val isEqualToString:#"2"]) {
[round2 addObject:loopObject];
} else if ([val isEqualToString:#"3"]) {
[round3 addObject:loopObject];
}
}

Array Issue in Objective C Xcode

I have a problem with my algorithm for calculating a sore. The user enters a word into the UITextField, and if the word matches a string in the array (#"The Word") the int 'score' will be added by 1.
Then the int score is set as a label as the user gets a word right. (DISPLAYING THE SCORE)
THE PROBLEM, a user can just keep on entering the same word over and over again and the score will keep going up by one. IS there a command for knowing if a word has already been entered, so you can only use the word once.
The Code
NSArray *scoreArray1 = [NSArray arrayWithObjects:
#"Word 1", #"Word 2", #"Word 3", nil];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:#"%d", myInt];
[_scorelabel setText:score];
}
UPDATE!!!!!!
NSArray *scoreArray1 = [NSArray arrayWithObjects:
#"Alan Shearer", #"Shearer", #"Andrew Cole", #"Andy Cole", #"Cole", #"Thierry Henry", #"Henry", #"Robbie Fowler", #"Fowler", #"Frank Lampard", #"Lampard", #"Michael Owen", #"Owen", nil];
NSSet *set2 = [NSSet setWithArray:scoreArray1];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in set2) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:#"%d", myInt];
[_scorelabel setText:score];
}
HOWEVER NOW THE APP DOES NOT WORK, IT CRASHES, any suggestions?
Why don't you keep a second Array where you store the given (correct) answers.
Whit this you can just do a contains inside your if....problem solved.
a second option is not to put string in your array but "Answer" Objects, that have a field that you can flag as already used.
You could just create an NSMutableSet and put a copy of the word into there whenever one is entered. Then you just need to check if the word exists in the set before incrementing the score.
I'm suggesting a set because it uses hashed access, so lookups are fast. Also, if you add the same string more than once, the set will still only have one reference to the string.
Actually, if you have an array of "legal" words, the way to go is to simply remove each word as it's called out, until the array gets to be zero entries long.
NSMutableArray* scoreArrayCopy = [NSMutableArray arrayWithArray:scoreArray];
int originalCount = scoreArrayCopy.count;
...
while (scoreArrayCopy.count > 0) {
NSString* guess = <get next guess>;
[scoreArrayCopy removeObject:guess];
score = originalCount - scoreArrayCopy.count;
}
(If you have a lot of words things would be more efficient if you used an NSMutableSet instead of an NSMutableArray, but the logic would be the same.)

Getting the size of an array

i have some code that requires the use of a for loop to read variables from an array.
int size=sizeof names;
NSLog(#"thelast one is %d",size);
NSString *usersName=userName.text;
NSString *usersPass=passWord.text;
for (i=0; i<=size;i++){
NSString *namesArray=[names objectAtIndex:i];
NSString *passArray=[pass objectAtIndex:i];
NSLog(#"namesArray %#",namesArray);
NSLog(#"passArray %#",passArray);
if([namesArray isEqualToString:usersName]){
userValid=1;
NSLog(#"The content of arry4 is %#",namesArray);
}
if([passArray isEqualToString:usersPass]){
passValid=1;
NSLog(#"The content of arry4 is %#",passArray);
}
else {
userValid=0;
passValid=0;
}
}
I've been having some problems because every time this function is called from within the program, it's almost as if the 'sizeof names' is wrong, therefore not all values in the array are checked.
I'm generally a Java programmer so i'm used to names.length, and i was told sizeof names is essentially the same thing... any help?
Cheers.
Don't use sizeof. Use [names count].
You want to use [names count] not sizeof names. Sizeof is going to give you the size of the actual names object pointer itself and not the number of elements, since it's dynamic memory type.
To get the number of elements stored in an NSAarray you should use the instance method count, which returns an NSUInteger.
Alternatevely, you can iterate over these elements using the for in loop, which is available also in Java, if I recall correctly.
for (MyClass *element in myArray) {
NSLog(#"%#", element);
}
Note that sizeof is a C operator that returns the size in bytes of its operand, so it doesn't tell you how many elements are stored in that NSArray, but the size in bytes of one NSArray instance.
I know your question has already been answered - but here is a more Cocoa way of writing it
NSString *userName = userName.text;
NSString *userPass = passWord.text;
// Use a block enumerator
NSUInteger nameIdx = [names indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return ([obj isEqualToString:userName]);
}];
// Is the name in the array
if (nameIdx == NSNotFound) {
// Name not in array - so set to zero
userValid = 0;
passValid = 0;
} else {
userValid = 1;
// See if the corresponding password is correct
NSString password = [pass objectAtIndex:nameIdx];
if (![password isEqualToString:userPass]) {
passValid = 0;
} else {
passValid = 1;
}
One can also use Fast Enumeration, in some cases it can be more clear to a reader:
NSString *userName = userName.text;
NSString *userPass = passWord.text;
BOOL userValid = NO;
BOOL passValid = NO;
int index = 0;
for (NSString *eachName in namesArray) {
if ([eachName isEqualToString:userName) {
userValid = YES:
if ([[passArray objextAtIndex:index] isEqualToString:passWord) {
passValid = YES;
}
break;
}
index += 1;
}