Remove items from array to ensure no duplicate strings

user3246508

I need to be able to pull 100's of strings from a plist and display them in a label within my project. I have managed to get this to work pulling data from two different arrays within my plist and then generating a random string to display.

What i want to do now is ensure that no two strings are displayed twice in one session and also be able to set a counter system up that after 5 goes it displays a message.

I was thinking of doing a simple counter for the display message after x amount of turns however when it comes to not displaying duplicates from the array im a little lost. I need it to only basically remove an item from the array (not the plist every time the user presses the button)

- (IBAction)truth:(id)sender {


NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"test" ofType:@"plist"];

NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray = plistDict[@"truth"];


NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray2 = plistDict2[@"dare"];

plistArray = [plistArray arrayByAddingObjectsFromArray:plistArray2];

NSLog(@"%@", plistArray);


int randV = arc4random() % plistArray.count;


self.joke.text = plistArray[randV];

NSLog(@"dictionary: %@, array: %@", plistDict, plistArray);

}

Jack

The problem you have is that every time you are getting a message, you are reading the plists again and recreating plistArray

The correct way would be to save plistArray into a local variable (a property) and only populate it the first time.

Then, if you want to remove an item, you call `[plistArray removeObjectAtIndex: randV];

Edit: code

// In .h file 
@property (strong, nonatomic) NSMutableArray * plistArray;


- (IBAction)truth:(id)sender {

if (!self.plistArray) {
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"test" ofType:@"plist"];

NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray * plistArray1 = plistDict[@"truth"];


NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[@"dare"];

self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];
}

NSLog(@"%@", plistArray);


int randV = arc4random() % self.plistArray.count;


self.joke.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
NSLog(@"dictionary: %@, array: %@", plistDict, self.plistArray);
}

@property

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a callback to remove duplicate items from an array

From Java

Kotlin - Idiomatic way to remove duplicate strings from array?

From Dev

Remove items from array

From Dev

The best way to remove duplicate strings in an array

From Dev

Remove duplicate strings in select params array

From Dev

removing adjacent duplicate strings from an array of strings?

From Java

Remove duplicate items from KeyValuePair List by Value

From Dev

Remove duplicate subcategory block items from xml

From Dev

Remove duplicate items by value from a dictionary

From Dev

Remove duplicate items from S3 objects multi dimension array in php

From Dev

Remove Duplicate Items From Multidimensional Array Using Javascript, jQuery, & underscore.js

From Dev

Remove duplicate items from S3 objects multi dimension array in php

From Dev

Is it possible to remove items from a array?

From Dev

How do you remove a certain set of characters, so that it does not duplicate, from an array of strings in Javascript or AngularJS?

From Dev

Remove numbers from strings in array

From Dev

Remove strings from PHP array

From Dev

Remove strings from PHP array

From Dev

Extract duplicate string from items in array

From Dev

Remove duplicate array value from array list

From Dev

Remove strings containing words from list, without duplicate strings

From Dev

Filter an array of strings from another array of strings in javascript (this is not a duplicate)

From Dev

Delete the duplicate words from array of strings in MySQL

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Remove items from one array if not in the second array

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Add array items based on a value and remove duplicate value in php

From Dev

Remove duplicate items in objects array with lodash.js

From Dev

Add array items based on a value and remove duplicate value in php

From Dev

Is there a functional way to remove duplicate items in a multi dimensional array in PHP

Related Related

  1. 1

    Using a callback to remove duplicate items from an array

  2. 2

    Kotlin - Idiomatic way to remove duplicate strings from array?

  3. 3

    Remove items from array

  4. 4

    The best way to remove duplicate strings in an array

  5. 5

    Remove duplicate strings in select params array

  6. 6

    removing adjacent duplicate strings from an array of strings?

  7. 7

    Remove duplicate items from KeyValuePair List by Value

  8. 8

    Remove duplicate subcategory block items from xml

  9. 9

    Remove duplicate items by value from a dictionary

  10. 10

    Remove duplicate items from S3 objects multi dimension array in php

  11. 11

    Remove Duplicate Items From Multidimensional Array Using Javascript, jQuery, & underscore.js

  12. 12

    Remove duplicate items from S3 objects multi dimension array in php

  13. 13

    Is it possible to remove items from a array?

  14. 14

    How do you remove a certain set of characters, so that it does not duplicate, from an array of strings in Javascript or AngularJS?

  15. 15

    Remove numbers from strings in array

  16. 16

    Remove strings from PHP array

  17. 17

    Remove strings from PHP array

  18. 18

    Extract duplicate string from items in array

  19. 19

    Remove duplicate array value from array list

  20. 20

    Remove strings containing words from list, without duplicate strings

  21. 21

    Filter an array of strings from another array of strings in javascript (this is not a duplicate)

  22. 22

    Delete the duplicate words from array of strings in MySQL

  23. 23

    KnockoutJS: Remove array of items from an array

  24. 24

    Remove items from one array if not in the second array

  25. 25

    KnockoutJS: Remove array of items from an array

  26. 26

    Add array items based on a value and remove duplicate value in php

  27. 27

    Remove duplicate items in objects array with lodash.js

  28. 28

    Add array items based on a value and remove duplicate value in php

  29. 29

    Is there a functional way to remove duplicate items in a multi dimensional array in PHP

HotTag

Archive