【问题标题】:Learning: Xcode LIST_FOREACH in XCode. What is the use of LIST_FOREACH? if there is no such foreach concept in objectiveC学习:XCode 中的 Xcode LIST_FOREACH。 LIST_FOREACH 有什么用?如果objectiveC中没有这样的foreach概念
【发布时间】:2017-12-24 13:50:27
【问题描述】:

在 XCode 中输入代码时,我发现了 LIST_FOREACH 这个词。 XCode 中 LIST_FOREACH 的目的是什么?为什么会显示?

既然XCode-Objective C中没有foreach等其他函数,那么LIST_FOREACH有什么用呢?

谁能教我Objective-c中LIST_FOREACH的目的是什么?当Objective-c中没有foreach概念时

欢迎任何答案:)

【问题讨论】:

  • 精确的 foreach() 是什么意思。您希望通过该方法实现什么目标?
  • Objective-C 中没有这种称为 foreach() 的方法/函数。
  • “我需要精确的 foreach()” - 用于什么目的?
  • 学习..我在学习
  • 你在学习什么需要知道不存在函数的语法?

标签: ios objective-c xcode loops syntax


【解决方案1】:

供您参考,在objective 或swift 中没有像foreach 这样的循环,比如c# 或其他语言。

但同样可以通过下面的代码实现

NSArray *a = [NSArray new];       // Any container class can be substituted

for(id obj in a) {                // Note the dynamic typing (we do not need to know the
                                  // Type of object stored in 'a'.  In fact, there can be
                                  // many different types of object in the array.

    printf("%s\n", [[obj description] UTF8String]);  // Must use UTF8String with %s
    NSLog(@"%@", obj);                               // Leave as an object
}

【讨论】:

    【解决方案2】:

    您是否尝试对数组进行快速枚举?你可以这样做 -

    for (id tempObject in myArray) {
        NSLog(@"Single element: %@", tempObject);
    }
    

    【讨论】:

      【解决方案3】:

      快速枚举语法:

      for (NSString* key in xyz) {
              id value = [xyz objectForKey:key];
              // do stuff
          }
      

      更多信息:

      https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Collections/Articles/Enumerators.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        • 2016-11-04
        • 2014-04-06
        • 2017-10-01
        相关资源
        最近更新 更多