【问题标题】:How to create random strings using NSArray in iPhone?如何在 iPhone 中使用 NSArray 创建随机字符串?
【发布时间】:2011-05-09 13:39:24
【问题描述】:

我想使用字典数组创建一个随机字符串。我想在用户运行应用程序时生成随机问题。我已经使用数组完成了随机字符串。但我无法使用字典数组创建随机字符串。那么如何使用字典创建随机字符串。每次运行应用程序时,字符串数组都会更改,那么如何生成随机字符串?

例如:

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

NSArray* pointStrings = [[NSArray alloc] initWithContentsOfFile:filePath];

我的实际数组是,

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

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

    } )

预期的数组,

随机数组(

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

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

谢谢。

【问题讨论】:

    标签: iphone random nsstring nsarray


    【解决方案1】:

    一个非常简单的解决方案是创建一个包含字典中所有字符串的临时数组。然后使用

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

    然后重复这个直到你满意为止。

    【讨论】:

      【解决方案2】:

      普格尔,

      使用 NSMutableArray 代替 NSArray,这样可以有更多的方法来访问,

      NSUInteger 计数 = [自计数];

      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];
      }
      

      查看链接以获取更多参考:What's the Best Way to Shuffle an NSMutableArray? 约翰逊在里面已经解释得很好了。

      【讨论】:

        【解决方案3】:

        我知道一个“窍门”。如果您使用 plist 文件,您可以像这样创建文件:

        <?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>
        

        之后,当您像这样重新加载文件时:

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];
        
        NSDictionary* allFile = [[NSDictionary alloc] initWithContentsOfFile:filePath];
        
        NSArray* pointStrings = [allFile allKeys];
        

        返回所有字典键但没有顺序......也许你可以使用它。 NSDictionary documentation

        【讨论】:

        • 您永远无法确定顺序的确定与随机(白噪声分布)选择相同...
        猜你喜欢
        • 1970-01-01
        • 2010-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        相关资源
        最近更新 更多