开发者

How to create random strings using NSArray in iPhone?

开发者 https://www.devze.com 2023-03-03 18:36 出处:网络
I want to create a random strings using array of Dictionary. I want to generate the random questions, when user runs the application. I had done with the random strings using arrays. But i couldn\'t c

I want to create a random strings using array of Dictionary. I want to generate the random questions, when user runs the application. I had done with the random strings using arrays. But i couldn't create random strings using array of dictionary. So how to create a random strings using dictionary. The array of strings would be changed every time, when runs the application, so how can i generate random strings?

For eg:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

NSArray* poi开发者_开发技巧ntStrings = [[NSArray alloc] initWithContentsOfFile:filePath];

My actual array is,

The array is (
        {
        id = 1;
        question = India;
        answer = Punjab;
       },

        {
         id = 2;
         question = Kolkatta;
         answer = "West Bengal";
    },
        {
        id = 3;
        question = Mumbai;
         answer = Maharastra;

    } )

Expected array,

The Random array(

         {
         id = 2;
         question = Kolkatta;
         answer = "West Bengal";
    },
        {
        id = 3;
        question = Mumbai;
         answer = Maharastra;

    },
        id = 1;
        question = India;
        answer = Punjab;
       })

Thanks.


A very simple solution is to create a temporary array with all the strings you have in your dictionary. Then use

int index = arc4random() % [tempArray count];
[randomArray addObject:[tempArray objectAtIndex:index]];
[tempArray removeObjectAtIndex:index];

And then repeat this until you are satisfied.


Pugal,

Use NSMutableArray instead of NSArray so that you can have more methods to access,

NSUInteger count = [self count];

for (NSUInteger i = 0; i < count; ++i) { 

    // Select a random element between i and end of array to swap with.       
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
}

Check the link for further reference: What's the Best Way to Shuffle an NSMutableArray? Johnson has already explained well in it.


I know a "trick" for that. If you use a plist file you can create your file like this :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>firstStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
    <key>secondStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
    <key>thirdStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
</dict>
</plist>

After that when you reload your file like this :

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

NSDictionary* allFile = [[NSDictionary alloc] initWithContentsOfFile:filePath];

NSArray* pointStrings = [allFile allKeys];

That return all dictionary keys but without order… Maybe you can use this. NSDictionary documentation

0

精彩评论

暂无评论...
验证码 换一张
取 消