我开始敲打我的应用程序并且已经深入了解它运行良好但我认为我让一些东西运行有点狂野.
对于每个@property(非原子,保留)然后我创建一个@synthesize我需要在dealloc方法中有[变量释放] - 这是正确的吗?
即使它是IBOutlet?
就像我理解的那样,@synthesize实际上是生成Getters和Setter.但是什么是@property呢?它只是为那个很酷的@synthesize魔术功能设置参数吗?
我在IOS模拟器中遇到了段错误11内存访问错误,但当我在下面的代码中注释掉该版本时,它就消失了.
// get get the question number
NSString *text = [attributeDict valueForKey:XML_TAG_QUESTION_ATTRIBUTE_NUMBER];
question.number = [text intValue];
//[text release]; <==== no more segfault 11 when this is commented out.
Run Code Online (Sandbox Code Playgroud)
我的问题是,因为我收到NSXMLParser实现返回的NS字符串的实例,引用计数是不是增加了,我不应该释放它吗?
我仍然是Objective-C编码的新手(正如这个问题所证明的那样),我想我并不完全理解在@property声明中如何使用retain属性.
这是一个示例类:
@interface Foo : NSObject {
NSMutableArray *myArray;
}
@property (retain) NSMutableArray *myArray;
Run Code Online (Sandbox Code Playgroud)
我的理解是,加入保留属性为@property申报(并使用在实现文件进行必要的@synthesize delcaration)将基本上做到以下setter和getter对我来说:
- (void)setMyArray:(NSMutableArray *)newArray {
myArray = [[NSMutableArray alloc] initWithArray:newArray];
[newArray release];
}
- (NSMutableArray *)myArray {
return myArray;
}
Run Code Online (Sandbox Code Playgroud)
这是准确的还是我误解了retain属性的工作原理?
我正在表视图中加载JSON数据.问题是,如果我滚动表视图,我的应用程序崩溃.以下是代码:
- (void)viewDidLoad {
[super viewDidLoad];
jsonurl=[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"];
jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];
jsonArray = [jsonData JSONValue];
items = [jsonArray objectForKey:@"items"];
story = [NSMutableArray array];
description1 = [NSMutableArray array];
media1 = [NSMutableArray array];
for (NSDictionary *item in items )
{
[story addObject:[item objectForKey:@"title"]];
[media1 addObject:[item objectForKey:@"media"]];
}
NSLog(@"booom:%@",story);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [story count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier …Run Code Online (Sandbox Code Playgroud) objective-c ×4
cocoa-touch ×1
ios ×1
ipad ×1
iphone ×1
properties ×1
scroll ×1
uikit ×1
uitableview ×1