2013-02-17 3 views
1

동일한 코드는 iOS 5에서는 작동하지만 iOS 6에서는 작동하지 않습니다. 개수는 0으로 표시됩니다. 어떤 아이디어입니까? house.png이 유효한 이미지로 확인되었으므로 적어도 1을 카운트로 표시해야합니다.싱글 톤에서 nsmutable 배열에 객체를 추가 할 수 없습니다.

#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 [email protected]""; 
    [email protected]""; 
    [email protected]""; 
    [email protected]""; 
    [self.assets removeAllObjects]; 
} 


@end 
+0

이상한 일입니다. house.png이 대상에없는 것일 수 있습니까? 이미지가 '복사 번들 리소스'단계에 있는지 확인 하시겠습니까? – Vinzzz

답변

3

assets 속성과 관련된 추가 ivars가 있습니다.

assets이라는 속성을 정의합니다. 그런 다음 (불필요하게) 생성 된 ivar의 이름을 _assets으로 지정하여 속성을 합성합니다.

또한 assets이라는 명시적인 ivar을 (불필요하게) 선언해야합니다.

init 방법에서는 배열을 assets ivar에 할당합니다. reset 메서드에서 assets 속성을 지우십시오 (_assets ivar 사용).

명시적인 assets ivar를 제거하십시오. @synthesize 성명을 삭제하십시오. 자동으로 생성 된 ivar가 _assets으로 남습니다.

속성 또는 _assets ivar를 사용하도록 코드를 업데이트하십시오.

+0

네가 맞습니다. 내가 할 때 _assets = [[NSMutableArray alloc] init]; 괜찮 았어. 재미있는 점은 ios 5에서 작동하는 동일한 코드입니다. 코드를 적절하게 수정합니다. – kratos

+0

NSMutableArray * 애셋을 제거했습니다. 지금 싱글 톤에서 self.assets를 사용하고 있습니다. – kratos

+2

iOS의 버전은 이것과 아무 관련이 없습니다. 이전 버전의 컴파일러 (Xcode의 이전 버전)와 다르게 작동했을 수도 있지만 정확한 코드가 주어지면 작동 방법이 없어야합니다. '@ synthesize' 라인을 삭제했다면 그대로 작동했을 것입니다. – rmaddy

1

보십시오 : 당신은 속성으로 사용자 인터페이스에있는 SharedObject를 정의

_assets = [[NSMutableArray alloc] init]; 

그리고 : MyManager *_sharedObject; 여기

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]); 

내 싱글 톤 : 여기

내 코드입니다

로컬 static var에 초기화 된 인스턴스를 보유하고있는 [MyManager sharedInstance];을 통해 항상 인스턴스를 가져 오는 경우에는 필요하지 않습니다.

관련 문제