不了解全局变量 [英] Don't understand global variables

查看:126
本文介绍了不了解全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPad开发一款应用程序,通常需要共享变量。对我来说,最简单的解决方案似乎是用全局变量创建一个类,而不是关注来回传递 - 因为我也遇到了一些问题。



在Objective-C中为iOS IOS7创建和使用全局变量的最简单方法是使用Xcode 5和故事板?



(我知道有重复但我可以'让它工作)

解决方案

编辑:关注@nhgrif和其他评论, em>



传递变量或使用全局变量的方式实际上取决于您试图做什么。



以下是几种方法:
$ b 将值从一个视图传递到另一个视图可以在添加到堆栈之前完成,如下所示:

/ p>

  ViewController * viewController = [[ViewController alloc] init]; 
viewController.yourIntegerValue = 42;
[self presentViewController:viewController animated:YES completion:nil];

NSUserDefaults 非常适合您希望通过的通用小数据块围绕或保存供以后使用。请参阅AlwaysWannaLearn的示例,我通常为所有通用值(使用键)构建和存储NSDictionary。
(另请参阅我同意的@nhgrif评论)



AppDelegate 解决方案是另一种方式,因为@Martin Koles建议。也许可以用一套价值,如果仅仅是为了将所有的全局变量放在一个地方,我会避免它。



Singleton 解决方案(原始答案)。
它简单,可扩展,优雅和高效。我通常会将其中一个添加到我创建的任何项目中。



快速操作:

创建一个新文件:MyManager.h

  #import< foundation / Foundation.h> 

@interface MyManager:NSObject {
NSString * someProperty;
}

@property(nonatomic,retain)NSString * someProperty;

+(id)sharedManager;

@end

创建一个新文件:MyManager.m

  #importMyManager.h

@implementation MyManager

@synthesize someProperty ;

#pragma标记Singleton方法
$ b $(b)(id)sharedManager {
static MyManager * sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(& onceToken,^ {
sharedMyManager = [[self alloc] init];
});
返回sharedMyManager;

$ b - (id)init {
if(self = [super init]){
someProperty = [[NSString alloc] initWithString:@Default Property值];
}
返回自我;
}

- (void)dealloc {
//永远不要被调用,但为了清晰起见,这里真的很简单。
}

@end

-
此类将保存您的全局函数和值



要使用这个漂亮的技巧,请将以下行粘贴到项目的其他任何位置 p>

  MyManager * sharedManager = [MyManager sharedManager]; 
// int getValuefromSingleton = sharedManager.MyFunctionOrValue;


I'm developing an app for iPad that often needs to share variables. Too me it seems like the easiest solution would be to create a class with global variables instead of focusing on passing back and forth - as I've also had some problems with that.

What's the easiest way to create and use global variables in objective-C for iPad IOS7 using Xcode 5 with storyboards?

(I know there are duplicates but I can't make it work)

解决方案

Edit: following @nhgrif and others comments, I am slightly changing my answer

The way to pass variables or use global variables really depends on what you try to do.

Here are few ways:

Passing a value from one view to the other can simply be done before adding it to the stack as so:

ViewController *viewController = [[ViewController alloc] init];
viewController.yourIntegerValue =  42;   
[self presentViewController:viewController animated:YES completion:nil];

NSUserDefaults are great for generic small chunks of data which you like to pass around or save for later use. see example from AlwaysWannaLearn, I usually build and store NSDictionary for all the generic value (with keys). (also see comment from @nhgrif which I agree with)

AppDelegate solution is another way to go as @Martin Koles suggested. Perhaps ok for a single set of value, I would avoid it if just for the sake of having all my globals in one place.

Singleton solution (original answer). it is simple, expandable, elegant and efficient. I usually add one of them to any project I create.

here is how to do it quickly:

create a new file : MyManager.h

#import <foundation/Foundation.h>

@interface MyManager : NSObject {
    NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedManager;

@end

create a new file : MyManager.m

#import "MyManager.h"

@implementation MyManager

@synthesize someProperty;

#pragma mark Singleton Methods

+ (id)sharedManager {
    static MyManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
  if (self = [super init]) {
      someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
  }
  return self;
}

- (void)dealloc {
  // Should never be called, but just here for clarity really.
}

@end

Thats it really - this class will hold your global functions and values

To use this nifty trick , paste the following line anywhere else in your project

MyManager *sharedManager = [MyManager sharedManager];
//int getValuefromSingleton = sharedManager.MyFunctionOrValue;

这篇关于不了解全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆