Guest User

Untitled

a guest
Jun 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2. #include <string>
  3. #include <vector>
  4.  
  5. @interface ObjcClass: NSObject
  6. @property (nonatomic, readonly) NSString *name;
  7. - (instancetype)initWithName:(NSString *)name;
  8. - (void)speak;
  9. @end
  10.  
  11. @implementation ObjcClass
  12. - (instancetype)initWithName:(NSString *)name {
  13. self = [super init];
  14. if (self) {
  15. _name = name;
  16. }
  17. return self;
  18. }
  19. - (void)dealloc { NSLog(@"%@ is deleted", self.name); }
  20. - (void)speak { NSLog(@"I am %@", self.name); }
  21. @end
  22.  
  23. class CppClass {
  24. public:
  25. std::vector<ObjcClass*> m_objcInstances; // ### Is ARC enabled? ###
  26. public:
  27. CppClass()
  28. : m_objcInstances() {
  29. for (int i = 0; i < 5; ++i) {
  30. auto name = [NSString stringWithFormat:@"Objc instance %d", i];
  31. m_objcInstances.push_back([[ObjcClass alloc] initWithName:name]);
  32. }
  33. }
  34. ~CppClass() { NSLog(@"CppClass is deleted"); }
  35. void Speak() const {
  36. for (auto objc : m_objcInstances) {
  37. [objc speak];
  38. }
  39. }
  40. };
  41.  
  42. int main(int argc, const char * argv[]) {
  43. ObjcClass *reference = nil;
  44. @autoreleasepool {
  45. NSLog(@"Hello, World!");
  46. const auto cppClass = CppClass();
  47. cppClass.Speak();
  48. reference = cppClass.m_objcInstances[2]; // retain
  49. }
  50. NSLog(@"End");
  51. return 0;
  52. }
Add Comment
Please, Sign In to add comment