【问题标题】:Short hand for [[UIApplication sharedApplication] delegate]?[[UIApplication sharedApplication] delegate] 的简写?
【发布时间】:2010-11-10 14:46:00
【问题描述】:

我将全局变量存储在我的 AppDelegate 中并通过以下方式访问它们:

AppDelegate *d = [[UIApplication sharedApplication] delegate];
d.someString = ...

有什么推荐的方法来节省一些打字,这样我就不需要一次又一次地AppDelegate *d = [[UIApplication sharedApplication] delegate];?谢谢!

【问题讨论】:

  • 将您的 AppDelege 导入您的 pch 文件。通过这样做,您无需在每个文件中都导入,您可以在项目中的任何位置获取此宏。
  • 请注意,如果您发现自己一次又一次地输入AppDelegate *d = [[UIApplication sharedApplication] delegate];,那可能是代码异味。你真的需要应用代理来处理应用代理的事情,还是将它用作全局状态的垃圾场?

标签: iphone objective-c delegates singleton


【解决方案1】:

正如 Shaggy Frog 所说,在 YourAppDelegate.h 文件中定义一个宏,示例如下:

#define AppDelegate (YourAppDelegate *)[[UIApplication sharedApplication] delegate]

然后您可以像这样在代码中获取应用委托:

[AppDelegate ......];

【讨论】:

  • 这个。这是最好的解决方案。
  • 不需要像这样用括号括起来吗:stackoverflow.com/questions/4549410/…?
  • 另外,请确保“AppDelegate”和“YourAppDelegate”是不同的词。有时“YourAppDelegate”,即您的应用代理的名称,默认为“AppDelegate”。
【解决方案2】:

由于您的应用委托从未真正改变,您可以创建一个您在应用委托代码中定义的外部,非常类似于 Mac OS X Cocoa 应用程序的 NSApp 外部。

因此,请在您的 AppDelegate 标头中定义外部(或您将在任何地方包含的其他内容):

extern AppDelegate* appDelegate;

然后创建它并在你的实现文件中设置它:

AppDelegate* appDelegate = nil;

// later -- i can't recall the actual method name, but you get the idea
- (BOOL)applicationDidFinishLaunchingWithOptions:(NSDictionary*)options
{
  appDelegate = self;
  // do other stuff
  return YES;
}

那么其他类就可以访问它了:

#import "AppDelegate.h"

// later
- (void)doSomethingGreat
{
  NSDictionary* mySettings = [appDelegate settings];
  if( [[mySettings objectForKey:@"stupidOptionSet"] boolValue] ) {
    // do something stupid
  }
}

【讨论】:

  • 我更喜欢这个解决方案,因为点符号访问器使用这个解决方案
【解决方案3】:

您可以创建一个 C 风格的宏并将其放在某个头文件中。

(至于将您的应用程序委托用作一个巨大的全局变量包罗万象,这又是另一天的咆哮。)

【讨论】:

    【解决方案4】:

    我创建了一个名为 UIApplication+delegatecategory,其中包含一些便利消息。让我的特定代表成为便利信息之一。因此,例如,如果我的应用委托被称为 MyAppDelegate,它将如下所示:

    UIApplication+delegate.h

    #import "MyAppDelegate.h"
    
    @interface UIApplication(delegate)
    + (MyAppDelegate *)thisApp;
    @end
    

    UIApplication+delegate.m

    #import "UIApplication+delegate.h"
    
    
    @implementation UIApplication(delegate)
    
    + (MyAppDelegate *)thisApp {
        return (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
    }
    
    @end
    

    在需要委托的类中,我这样做:

    #import "UIApplication+delegate.h"
    
    ...
    
    - (void)doStuff {
        MyAppDelegate *app = [UIApplication thisApp];
        // use "app"
    }
    

    【讨论】:

      【解决方案5】:

      我还创建了一个类别,只是我将我的类别应用于 NSObject,因此应用程序中的任何对象都可以轻松获取委托。


      #import "MyAppDelegate.h"
      
      @interface NSObject(delegate)
      - (MyAppDelegate *) appDelegate;
      @end
      

      #import "NSObject+delegate.h"
      
      @implementation UIApplication(delegate)
      
      - (MyAppDelegate *)appDelegate {
          return (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 2014-05-28
        • 1970-01-01
        相关资源
        最近更新 更多