在编写c ++ 20年之后,我对Objective-C还是陌生的。 
在我看来,objective-C在松耦合消息传递方面很出色,但对数据管理却很糟糕。 
想象一下,我多么高兴地发现xcode 4.3支持Objective-C ++!
因此,现在我将所有.m文件重命名为.mm(编译为Objective-c ++),并使用c ++标准容器进行数据管理。
因此,“弱指针数组”问题成为__weak对象指针的std :: vector:
#include <vector>
@interface Thing : NSObject
@end
std::vector<__weak Thing*> myThings;
Thing* t = [Thing new];
myThings.push_back(t);
for(auto weak : myThings) {
  Thing* strong = weak; 
  if (strong) {
    
  }
}
等效于c ++习惯用法:
std::vector< std::weak_ptr<CppThing> > myCppThings;
std::shared_ptr<CppThing> p = std::make_shared<CppThing>();
myCppThings.push_back(p);
for(auto weak : myCppThings) {
  auto strong = weak.lock(); 
  if (strong) {
    
  }
}
概念证明(鉴于汤米对向量重新分配的关注):
main.mm:
#include <vector>
#import <Foundation/Foundation.h>
@interface Thing : NSObject
@end
@implementation Thing
@end
extern void foo(Thing*);
int main()
{
    
    std::vector<__weak Thing*> myThings;
    
    Thing* t = [[Thing alloc]init];
    for (int i = 0 ; i < 100000 ; ++i) {
        myThings.push_back(t);
    }
    
    foo(myThings[5000]);
    t = nullptr;
    foo(myThings[5000]);
}
void foo(Thing*p)
{
    NSLog(@"%@", [p className]);
}
日志输出示例:
2016-09-21 18:11:13.150 foo2[42745:5048189] Thing
2016-09-21 18:11:13.152 foo2[42745:5048189] (null)