0

这是我定义的一个块,它比较了 2 个 UIView:

typedef NSComparisonResult(^UITagCompareBlock)(UIView*, UIView*);

UITagCompareBlock uiTagCompareBlock = ^NSComparisonResult(UIView* a, UIView* b){

  if (a.tag < b.tag) return NSOrderedAscending;
  else if (a.tag > b.tag) return NSOrderedDescending;
  else return NSOrderedSame;
};

我通过以下方式使用它来对 UIView 数组进行排序:

self.arrayOfViews = [self.arrayOfViews sortedArrayUsingComparator: uiTagCompareBlock];

一切正常,但是如果我尝试将此块和 typedef 定义旋转到它自己的文件中,以便我可以在整个项目中使用相同的块,我会在编译时得到重复的符号错误。我怎样才能在整个项目中使用它?

4

1 回答 1

3

也许您在 .h 文件中定义了块,以便在导入此 .h 文件的每个 .m 文件中定义它?

您必须在 .h 文件中声明它:

typedef NSComparisonResult(^UITagCompareBlock)(UIView*, UIView*);
extern UITagCompareBlock uiTagCompareBlock;

并将其定义在一个 .m 文件中:

UITagCompareBlock uiTagCompareBlock = ^NSComparisonResult(UIView* a, UIView* b){

  if (a.tag < b.tag) return NSOrderedAscending;
  else if (a.tag > b.tag) return NSOrderedDescending;
  else return NSOrderedSame;
};
于 2013-08-15T12:11:10.847 回答