0

iPhone開発初心者です。

+ (id<GMGridViewLayoutStrategy>)strategyFromType:(GMGridViewLayoutStrategyType)type
{
    id<GMGridViewLayoutStrategy> strategy = nil;

    switch (type) {
        case GMGridViewLayoutVertical:
            strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontal:
            strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontalPagedLTR:
            strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontalPagedTTB:
            strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
            break;
    }

    return strategy;
}

ここで私はそのメソッドを呼び出しています:

gmGridView = [[GMGridView alloc] init];
gmGridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedLTR];
[self.view addSubview:gmGridView];

今私の質問は、strategyFromType メソッドの戦略オブジェクトをどのように解放するかということです。潜在的なリークが発生します。また、解放/自動解放しようとすると、アプリケーションがクラッシュします。助けてください よろしくお願いします...

4

4 に答える 4

4
return [strategy autorelease];

UPDATE:
The answer about returning an autoreleased object is correct, the problem is that the GMGridView uses ARC according to the description in the project's site.

Requirements:

iOS 4 and up
Xcode 4.2 (GMGridView uses ARC)
Frameworks: Foundation, UIKit, CoreGraphics and QuartzCore

So i suppose you need to add it the project as sub module, but you can search a little about instructions...

于 2012-06-04T09:58:10.107 に答える
1

使用している場合ARC、コードは問題ありませARCんが、自動解放されたオブジェクトを返す必要はありません。

+ (id)strategyFromType:(GMGridViewLayoutStrategyType)type { 
    id strategy = nil;

   switch (type) {
         case GMGridViewLayoutVertical:
                strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
         break;
         case GMGridViewLayoutHorizontal:
             strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
        break;
        case GMGridViewLayoutHorizontalPagedLTR:
            strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
        break;
        case GMGridViewLayoutHorizontalPagedTTB:
          strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
        break;
   }

   return [strategy autorelease];
}

メソッドによって返されるすべてのオブジェクトは、 if 、およびすべてのメソッドをautorelease除いて、 である必要があります。allocnewcopy

Advanced Memory Management Programming Guideを読むことを強くお勧めします。

于 2012-06-04T10:00:46.640 に答える
0

[戦略の自動リリース]を返します。また、[gmGridViewリリース];

関数の最後に

于 2012-06-04T10:34:50.580 に答える
0

As you are returning the object you need to autorelease the object

return [strategy autorelease];
于 2012-06-04T09:59:54.217 に答える