How do I add another object to my singleton code? - objective-c

Here is my singleton code:
#synthesize listOfSites;
+ (id)sharedInstance
{
static dispatch_once_t dispatchOncePredicate = 0;
__strong static id _sharedObject = nil;
dispatch_once(&dispatchOncePredicate, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
-(id)init
{
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
}
return self;
}
#end
It's pretty much textbook... however, I want to add another array, similiar to "listOfSites" (call it "listOfReadings"). The code that says "if (self)" confuses me.
How do I add another array to this code?

if (self) { does nothing but to verify that the [super init] has worked - and that it hasn't return NULL or anything...
Other than that, you can do this normally, like :
declare a listOfReadings array (as an ivar/property?)
set it up
listOfReadings = [[NSMutableArray alloc] init];
or
listOfReadings = [[NSMutableArray alloc] initWithObjects:nil];
or (if it's a property)
[self setListOfReadings:[[NSMutableArray alloc] initWithObjects:nil]];
Example :
-(id) init {
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
listOfReadings = [[NSMutableArray alloc] init];
}
return self;
}
after having declared the new NSMutableArray in your .h file :
NSMutableArray* listOfReadings;

-(id) init {
self = [super init];
if (self) {
listOfSites = [[NSMutableArray alloc] init];
listOfReadings = [[NSMutableArray alloc] init];
}
return self;
}
if (self) in other words means "If current object is successfully created then..."

Related

Can I initiate an ivar indirectly?

I'm trying to initiate my ivar like this:
Declared like this in h-file
#interface MyClass: {
UITextView *_myTextView;
}
then created like this in the m-file
- (id)init {
self = [super init];
if(self) {
[self initTextView:_myTextView];
}
return self;
}
- (void)initTextView:(UITextView *)textView {
textView = [[UITextView alloc] init];
...
}
_myTextView will still be nil afterwards. Why is that and what should I do it to make it work? I've got ARC enabled.
[EDIT]
This works. Thanks all!
- (id)init {
self = [super init];
if (self) {
_textView1 = [self createTextView];
_textView2 = [self createTextView];
_textView3 = [self createTextView];
}
return self;
}
- (UITextView *)createTextView {
UITextView *textView = [[UITextView alloc] init];
...
return textView;
}
You need to always refer to instance variables using:
self.textView = [[UITextView alloc] init];
Also use a name other than initTextView as methods starting with init have special meaning in Objective-C.
If you want to use the same code to initialize multiple text view controls, then use code like this:
- (UITextView *)createTextView
{
UITextView *textView = [[UITextView alloc] init];
textView.something = whatever;
...
return textView;
}
And then use it like this:
- (id)init {
self = [super init];
if(self)
{
self.textView1 = [self createTextView];
self.textView2 = [self createTextView];
...
self.textViewN = [self createTextView];
}
}
In [self initTextView:_myTextView]; you pass the current value of _myTextView (which is nil) to your initTextView: method. To set the instance variable, you need a pointer to a pointer.
- (id)init {
self = [super init];
if (self) {
[self setupTextView:&_myTextView];
}
return self;
}
- (void)setupTextView:(UITextView * __strong *)textView {
*textView = [[UITextView alloc] init];
...
}
I also renamed the initTextView: method to setupTextView, as methods starting with init are expected to behave like other init methods in ARC.
- (id)init {
self = [super init];
if(self) {
[self initTextView];
}
}
- (void)initTextView{
_myTextView = [[UITextView alloc] init];
...
}
if you want to call initTextView for several text views , you can code like this :
- (id)init {
self = [super init];
if(self) {
_myTextView = [[UITextView alloc] init];
[self initTextView:_myTextView];
}
}
- (void)initTextView:(UITextView *)textView{
//setup the textView
...
}

Why is NSMutableArray showing count size of 0?

Very confused here...
I have a NSObject subclass called "Section" with a NSMutableArray property called "content"
Section *sectionName = [[Section alloc] init];
[[sectionName content] addObject:#"test"];
[[sectionName content] addObject:#"test2"];
[[sectionName content] addObject:#"test3"];
NSLog(#"COUNT IS %i", [[sectionName content] count]);
Why is my NSLOG showing "COUNT IS 0"??
Are you initializing content in your subclass? If not, that can be the problem!
Your init method should look like this:
- (id)init
{
if (self = [super init])
{
_content = [[NSMutableArray alloc] init];
}
return self;
}
If you're not using ARC your dealloc should look like this:
- (void)dealloc
{
[_content release];
[super dealloc];
}
Are you initializing array content inside Section? are you doing something like - content = [[NSMutableArray alloc] init]; ?
Lemme know if this helps.
You probably need to allocate your NSMutableArray -- that is normally what causes this issue.
In your Section.m:
- (id) init {
//...
content = [[NSMutableArray alloc] init];
//...
}

Potential Leak of an object allocated

+ (WAController*) sharedWAController {
#synchronized([WAController class]) {
if (!_sharedWAController)
[[self alloc] init];
return _sharedWAController;
}
}
This is show in potential leak
for [[self alloc] init];
Why its leak ?
You never assign it to anything so it will just allocate a new object and leak it. To fix the leak and the incorrectly working code assign _sharedWAController
if (!_sharedWAController)
_sharedWAController = [[self alloc] init];
make it : _sharedWAController = [[self alloc] init];
+ (WAController*) sharedWAController {
#synchronized([WAController class]) {
if (!_sharedWAController)
{
_sharedWAController = [[self alloc] init];
}
return _sharedWAController;
}
}
+ (CommonUtility*) sharedUtility {
#synchronized([CommonUtility class]) {
if (!_sharedUtility)
_sharedUtility = [[self alloc] init];
return _sharedUtility;
}
}
+ (id) alloc {
#synchronized([CommonUtility class]) {
_sharedUtility = [super alloc];
return _sharedUtility;
}
}
I try like this is this perfect. But I allocated _sharedUtility object where i need to release. this In dealloc or autorelesae it.

Finding the cause of EXC_BAD_ACCESS

I have a class with the following init method:
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
[s release];
}
return self;
}
And StateStack has the following init code:
- (id)init {
self = [super init];
if (self) {
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
[s release];
NSLog(#"%d",[stack retainCount]);
}
return self;
}
Oddly, if I remove the NSLog line, the EXC_BAD_ACCESS moves to StateStack's dealloc method:
- (void)dealloc {
[stack release]; //<--EXC_BAD_ACCESS
[super dealloc];
}
Searching around seems to suggest that EXC_BAD_ACCESS is caused by overreleasing, but I can't see how I've overreleased anything. Does anyone know what the cause might be?
In your init function:
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
[s release];
you are allocating an instance of StateStack; this gets a retain count of 1. Then at the end of the function you call release, retain count goes to 0 and the object is ready to be released. So, when later dealloc is executed, the state ivar is sent another release and that is causing the bad access. You don't need to release s, since you want that state be retained. The same error pattern occurs in the other init method.
This would be correct:
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
StateStack* s = [[StateStack alloc] init];
state = s;
[s push:NONE]; //<--EXC_BAD_ACCESS on load here
}
return self;
}
- (id)init {
self = [super init];
if (self) {
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
}
return self;
}
NB: I don't want to generate misunderstandings. Using retain count to check for correct memory allocation is useless. This is true. Anyway, reasoning in terms of retain count helps understanding what happens when you allocate/release/autorelease an object. It is the basic mechanism, but it is too difficult to track it usage to check for correctness of memory management.
state = s is not copying the NSMutableArray object, it's just copying the pointer to it. So when you call [s release] the object referred to by both s and state is deallocated. You'll get an EXC_BAD_ACCESS whenever you use either from that point on.
Also, don't use [object retainCount] to debug memory management problems. It lies. Google NSZombies.
- (id)init{
self = [super init];
if (self) {
// Initialization code here.
state = [[StateStack alloc] init];
[state push:NONE];
}
return self;
}
StateStack
- (id)init {
self = [super init];
if (self) {
stack = [[NSMutableArray alloc] init];
}
return self;
}

Also found '-(void) init'

I built a custom class named game:
.h
-(void) init;
here I have a Also found '-(void) init'
.m
-(void) init {
[super init];
score = 0;
lives = 3;
elements = [[NSMutableArray alloc] initWithCapacity:1000];
}
when I try to initialize a object with:
myGame = [[Game alloc] init];
I got "Multiple methods named '-init' found
So I don't know where the error is...
init should always return (id). Change your function to the following:
.h
-(id) init;
.m
-(id) init {
if((self = [super init]))
{
score = 0;
lives = 3;
elements = [[NSMutableArray alloc] initWithCapacity:1000];
}
return self;
}