I made an array in a singleton to write objects into it from multiple parts of my code. Here's how:
// in singleton.h
#import <UIKit/UIKit.h>
// make globally accessible array
@interface MyManager : NSObject {
NSMutableArray *imgArray;
}
@property (nonatomic, retain) NSMutableArray *imgArray;
+ (id)sharedManager;
@end
// in singleton.m
#import "singleton.h"
For my .m file :
@implementation MyManager
@synthesize imgArray;
#pragma mark Singleton Methods
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id) init {
if (self = [super init]) {
self.imgArray = [NSMutableArray new];
}
NSLog(@"initialized");
return self;
}
@end
I can access my array called imgArray it from my objective C code. However, In swift I get an error when I do this:
let array = MyManager.sharedManager()
array.imgArray.add("hello world") . (!!!) Value of type 'Any?' has no member 'imgArray'
I can access MyManager.sharedManager()
, but Why can't I access imgArray
the same way as in objective C?