Objective c 我在哪里为iOS应用程序创建全局变量?
这是我的密码: 我希望能够创建一个全局NSMutableArray,它可以存储Budget*对象,然后将这些对象写入.pList文件。。。我只是在学习什么是PLIST,我对如何实现它们有点模糊 我哪里出了问题Objective c 我在哪里为iOS应用程序创建全局变量?,objective-c,ios,global-variables,Objective C,Ios,Global Variables,这是我的密码: 我希望能够创建一个全局NSMutableArray,它可以存储Budget*对象,然后将这些对象写入.pList文件。。。我只是在学习什么是PLIST,我对如何实现它们有点模糊 我哪里出了问题 - (IBAction)btnCreateBudget:(id)sender { Budget *budget = [[Budget alloc] init]; budget.name = self.txtFldBudgetName.text; budget.a
- (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"];
}
编辑:我想补充一点,我正在创建一个数组,以便其他视图可以访问它。我知道这可以通过通知来完成?还是应该在appDelegate类中执行此操作?最终目标是让anArray对象填充单独视图中的UITableView。在这种情况下,不需要全局变量。您可以这样做:
initWithContentsOfFile:
)但代码中的第二个问题是,如果预算类不是属性列表类型(NSString、NSData、NSArray或NSDictionary对象)
writeToFile:
将无法成功保存它。在这种情况下不需要全局变量。您可以这样做:
initWithContentsOfFile:
)但是代码中的第二个问题是,如果Budget类不是属性列表类型(NSString、NSData、NSArray或NSDictionary对象)
WriteFile:
将无法成功保存它。只需将声明放在方法外部而不是内部
NSMutableArray *anArray = nil;
- (IBAction)btnCreateBudget:(id)sender
{
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
如果仅在一个文件中使用,请将其设置为“静态”,以防止与其他文件发生名称冲突:
static NSMutableArray *anArray = nil;
如果只在一个方法中使用,则将其设置为“静态”,并将其放置在该方法中:
- (IBAction)btnCreateBudget:(id)sender
{
static NSMutableArray *anArray = nil;
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
请注意,人们通常对全局变量使用某种命名约定,如“gArray”,以便轻松地将它们与局部变量、实例变量或方法参数区分开来 只需将声明放在方法外部而不是内部
NSMutableArray *anArray = nil;
- (IBAction)btnCreateBudget:(id)sender
{
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
如果仅在一个文件中使用,请将其设置为“静态”,以防止与其他文件发生名称冲突:
static NSMutableArray *anArray = nil;
如果只在一个方法中使用,则将其设置为“静态”,并将其放置在该方法中:
- (IBAction)btnCreateBudget:(id)sender
{
static NSMutableArray *anArray = nil;
...
if ( anArray == nil )
anArray = [[NSMutableArray alloc] init];
...
}
请注意,人们通常对全局变量使用某种命名约定,如“gArray”,以便轻松地将它们与局部变量、实例变量或方法参数区分开来 您需要确保预算类调用
NSCoder
,然后调用NSCoder initWithCoder:
和NSCoder decodeWithCoder:
方法。否则,writeToFile:
将不适用于您的NSObject
class
但我离题了。原问题的答案如下
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
在.h
文件中,您需要执行以下操作
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
然后,您需要确保您合成了NSMutableArray
,这样您就不会收到任何异常的警告。这是在.m
文件中的@implementation
行之后完成的
然后,在您希望将其分配到内存中的函数中,只需执行以下操作
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
这现在是一个
全局变量。它是全局的
,因为它可以从任何函数中使用,而不限于在一个函数中使用。您需要确保预算类调用NSCoder
,然后调用NSCoder initWithCoder:
和NSCoder decoder withcoder:
方法。否则,writeToFile:
将不适用于您的NSObject
class
但我离题了。原问题的答案如下
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
在.h
文件中,您需要执行以下操作
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
然后,您需要确保您合成了NSMutableArray
,这样您就不会收到任何异常的警告。这是在.m
文件中的@implementation
行之后完成的
然后,在您希望将其分配到内存中的函数中,只需执行以下操作
@interface WhateverClassName : UIViewController
{
NSMutableArray *anArray;
}
@property(nonatomic, retain) NSMutableArray *anArray;
@end
anArray = [[NSMutableArray alloc] initWithObjects:nil];
这现在是一个全局变量。它是global
,因为它可以从任何函数中使用,而不限于在一个函数中使用。如果您希望整个应用程序或上下文都可以访问数据(“global”),可以使用单例。但是,要小心操作,并确保它实际上是必要的和适当的。我建议在实现任何单例之前对其进行大量的阅读。Carter Allen有一个很好的基本实现
根据“最终目标是让anArray对象填充单独视图中的UITableView”,您不需要向文件、数据库或单例写入任何内容。只需设置对象。就像塞巴斯蒂安·皮克所说的
如果希望脱机数据存储,请查看sqlite、json、plist等如果希望整个应用程序或上下文(“全局”)可以访问数据,可以使用单例。但是,要小心操作,并确保它实际上是必要的和适当的。我建议在实现任何单例之前对其进行大量的阅读。Carter Allen有一个很好的基本实现
根据“最终目标是让anArray对象填充单独视图中的UITableView”,您不需要向文件、数据库或单例写入任何内容。只需设置对象。就像塞巴斯蒂安·皮克所说的
如果您希望脱机数据存储,请查看sqlite、json、plist等为什么您希望它是全局的而不是实例变量?这并不是说我反对plist,而是您考虑过其他存储格式,例如json(使用JSONKit)?如果您计划为其他设备(android)编写此应用程序,JSON可能是更好的选择