我在哪里为iOS应用程序创建全局变量? [英] Where do I create global variables for an iOS app?
问题描述
以下是我的代码:
我希望能够创建一个全局NSMutableArray,它可以存储可以写入.pList文件的Budget *对象。 ..我只知道pLists是什么,我对如何实现它们有点朦胧......
我在哪里错了?
- (IBAction)btnCreateBudget:(id)sender
{
Budget * [= [[Budget alloc] init ]。
budget.name = self.txtFldBudgetName.text;
budget.amount = [self.txtFldBudgetAmount.text intValue];
//将数据写入pList
NSMutableArray * anArray = [[NSMutableArray alloc] init]; //我希望这是整个应用程序的全局变量。我在哪里放这个?
[anArray addObject:budget];
[anArray writeToFile:[self dataFilePath] atomically:YES];
/ *如您所见,以下是测试代码的地方。不幸的是,每次运行这个命令时,
都只有1个元素。我假设
,这是因为每次按下按钮时,我都会创建一个全新的
NSMutableArray * anArray。我希望这是整个应用程序的全球性。 * /
int i = 0;
(aArray中的Budget * b)
{
i ++;
}
NSLog(@anArray中有%d个项目,i); (NSDocumentDirectory,NSUserDomainMask,YES);
$ b -
NSString * documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@BudgetData.plist];
$编辑:我想补充说我创建了anArray数组它可以被其他视图访问。我知道这可以通过NSNotification完成?或者我应该这样做的AppDelegate类?最终目标是让anArray对象填充位于单独视图中的UITableView。解决方案只需将声明放在方法而不是内部。
NSMutableArray * anArray = nil;
- (IBAction)btnCreateBudget:(id)sender
{
...
if(anArray == nil)
anArray = [[NSMutableArray alloc] init];
...
}
如果仅在一个文件中使用它,使其成为静态来防止与其他文件的名称冲突:
static NSMutableArray * anArray = nil;
如果仅在一个方法中使用它,请将其设置为static并将其放入该方法中:
- (IBAction)btnCreateBudget:(id)sender
{
static NSMutableArray * anArray = nil;
...
if(anArray == nil)
anArray = [[NSMutableArray alloc] init];
...
}
请注意,人们通常会使用某种命名全局变量的约定,比如gArray,可以很容易地将它们与局部变量,实例变量或方法参数区分开来。
Here is my code:
I want to be able to create a global NSMutableArray that can store Budget* objects that can then be written to a .pList file... I'm only learning what pLists are, and I am a bit hazy about how to implement them...
Where am I going wrong here?
- (IBAction)btnCreateBudget:(id)sender
{
Budget *budget = [[Budget alloc] init];
budget.name = self.txtFldBudgetName.text;
budget.amount = [self.txtFldBudgetAmount.text intValue];
// Write the data to the pList
NSMutableArray *anArray = [[NSMutableArray alloc] init]; // I want this to be a global variable for the entire app. Where do I put this?
[anArray addObject:budget];
[anArray writeToFile:[self dataFilePath] atomically:YES];
/* As you can see, below is where I test the code. Unfortunately,
every time I run this, I get only 1 element in the array. I'm assuming
that this is because everytime the button is pressed, I create a brand new
NSMutableArray *anArray. I want that to be global for the entire app. */
int i = 0;
for (Budget * b in anArray)
{
i++;
}
NSLog(@"There are %d items in anArray",i);
}
-(NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"BudgetData.plist"];
}
edit: I'd like to add that I am creating the anArray array so that it can be accessible by other views. I understand that this can be done with NSNotification? or Should I do this the appDelegate classes? The end goal is to have the anArray object populate a UITableView that is in a separate View.
解决方案 Just put the declaration outside the method instead of inside it.
NSMutableArray *anArray = nil;
- (IBAction)btnCreateBudget:(id)sender
{
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
If it's only used inside the one file, make it "static" instead to prevent name collisions with other files:
static NSMutableArray *anArray = nil;
If it's only used inside the one method, make it "static" and put it inside that method:
- (IBAction)btnCreateBudget:(id)sender
{
static NSMutableArray *anArray = nil;
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
Note that people usually use some kind of naming convention for global variables, like "gArray", to easily differentiate them from local variables, instance variables, or method parameters.
这篇关于我在哪里为iOS应用程序创建全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!