【问题标题】:Load Random VOID method加载随机 VOID 方法
【发布时间】:2015-10-11 00:59:46
【问题描述】:

我创建了一个内容视图,用于从JSon获取新闻,但我想随机加载。

我有两种方法:

- (void)radioNews {}
- (void)topNews {}

现在我想在我的viewDidLoad 中获取随机的 radioNews 或 topNews

    - (void)viewDidLoad {
           [self radioNews];
    }

这仅对加载一种方法是正确的,可以从radioNewstopNews 告诉getRandom,然后在didLoad [self methodResult] 中告诉getRandom 吗?

谢谢

【问题讨论】:

  • 我怀疑radioNewstopNews 非常相似,只是在某些搜索参数方面有所不同。因此,确定不同之处并随机化该数据,而不是调用的方法。你肯定是在复制很多代码。
  • 是两个不同的json服务器和不同的数组,所以做两个方法更简单快捷。你给我-1作为你的假设?程序员的世界被像你这样的人毁了,无论如何谢谢你的提示

标签: ios objective-c methods void


【解决方案1】:

试试

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInteger random = arc4random()%2;
    if (random == 0) {
        [self radioNews];
    }else{
        [self topNews];
    }
}

【讨论】:

    【解决方案2】:

    您可以从NSString 调用selector 开始使用NSString 创建NSArray

    // Define an NSArray of NSString, where a single string is the method name
    NSArray *methods = @[@"radioNews", @"topNews"];
    // Retrieve a random index
    NSUInteger index = arc4random()%methods.count;
    // You can use objectAtIndex without check the array bounds because
    // the mod operation you did before will always return a value in bounds
    
    // Create a selector instance from a string
    SEL randomSelector = NSSelectorFromString([methods objectAtIndex: index]);
    // Call the selector
    [self performSelector:randomSelector 
               withObject:nil];
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2012-07-01
      • 2018-12-22
      相关资源
      最近更新 更多