1

The same code works on iOS 5 but not in iOS 6. That is the count shows as being 0. Any ideas? It should say atleast 1 as the count since I have verified house.png to be a valid image.

Here is my code:

MyManager * myManager = [MyManager sharedInstance];
                NSString *pathOfImageFile = [[NSBundle mainBundle] pathForResource:@"house" ofType:@"png"];
                UIImage *myImage = [UIImage imageWithContentsOfFile:pathOfImageFile];

                UIImageView * tempImageView = [[UIImageView alloc] initWithImage:myImage];
                [myManager.assets addObject:tempImageView];

                NSLog(@"image count: %d", [myManager.assets count]);

Here is my singleton:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyManager : NSObject
{

    MyManager *_sharedObject;
    NSMutableArray * assets;


}

//Property Listing
@property(nonatomic,copy) NSString * postTitle;
@property(nonatomic,copy) NSString * postText;
@property(nonatomic,copy) NSString * postLink;
@property(nonatomic,copy) NSString * postCategory;
//assets
@property (nonatomic, strong) NSMutableArray * assets;


+ (id)sharedInstance;
- (void)reset;



@end





#import "MyManager.h"

@implementation MyManager

//Property Listing
@synthesize postTitle=_postTitle;
@synthesize postText=_postText;
@synthesize postLink=_postLink;
@synthesize postCategory=_postCategory;
@synthesize assets=_assets;


- (id)init
{
    self = [super init];
    if ( self )
    {
         assets = [[NSMutableArray alloc] init];
        NSLog(@"Singleton Initialized...");
    }
    return self;
}




+ (id)sharedInstance
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
}



- (void)reset
{

    self.postTitle =@"";
    self.postText=@"";
    self.postLink=@"";
    self.postCategory=@"";
    [self.assets removeAllObjects];
}


@end
1
  • That's weird. Maybe house.png isn't in the target ? are you sure the image is still in the 'copy bundle resources' phase ? Commented Feb 17, 2013 at 17:57

2 Answers 2

3

You have extra ivars related to your assets property.

You define a property named assets. You then (needlessly) synthesize the property specifying that the generated ivar should be named _assets.

You also (needlessly) declare an explicit ivar named assets.

In your init method you assign the array to the assets ivar. In your reset method you clear the assets property (using the _assets ivar).

Get rid of the explicit assets ivar. Get rid of the @synthesize statement. This will leave you with an auto generated ivar of _assets.

Update your code to use either the property or the _assets ivar.

Sign up to request clarification or add additional context in comments.

4 Comments

yes you are correct. When I did _assets = [[NSMutableArray alloc] init]; it worked fine. Funny thing is the same code worked on ios 5. I will fix my code appropriately.
I got rid of the NSMutableArray * assets; and am using self.assets in the singleton now
The version of iOS has nothing to do with this. It's possible it may have worked differently with an older version of the compiler (from older versions of Xcode) but even then, given the exact code, there should be no way it ever worked. It would have worked as-is if you just removed the @synthesize line.
You are correct. i just checked and I was using "@synthesize assets" and not "@synthesize assets=_assets" which I tweaked in this app. Thanks much gentlemen (or ladies)
1

Try:

 _assets = [[NSMutableArray alloc] init];

And you are defining sharedObject in your interface as a property: MyManager *_sharedObject;

You don't want that if you'll always grab your instance via [MyManager sharedInstance]; which is holding the initialized instance in that local static var.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.