这是我定义的一个块,它比较了2个UIViews:
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;
};
我以下面的方式使用它来排序UIViews数组:
self.arrayOfViews = [self.arrayOfViews sortedArrayUsingComparator: uiTagCompareBlock];
一切正常,但如果我尝试将此块和typedef定义旋转到自己的文件中,那么我可以在整个项目中使用相同的块,编译时会出现重复的符号错误。我怎样才能在整个项目中使用它?
答案 0 :(得分:3)
也许您在.h文件中定义了块,因此它在每个.m文件中定义 导入这个.h文件?
您必须在.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;
};