我还在继续svn客户端的项目。我在objective-c对象体外,有个静态的c语言的回调方法。其中,我将char*转化成了NSString,调试时一切正常。但运行到后面的时候,字符串变量会丢掉,并且只要一取出这个属性,就报错。
char* 转换到NSString的代码:
static svn_error_t *
my_list_repo_func (void *baton,
const char *path,
const svn_dirent_t *dirent,
const svn_lock_t *lock,
const char *abs_path,
apr_pool_t *pool)
{
printf ("%s\n",path);
SrcEntry *srcEntry = [[SrcEntry alloc]init];
// 注:后面取这个name属性时会报错
srcEntry.name = [NSString stringWithUTF8String:path];
srcEntry.path = [NSString stringWithUTF8String:abs_path];
srcEntry.lastAuthor = [NSString stringWithUTF8String:dirent -> last_author];
srcEntry.time = [NSNumber numberWithLong:dirent -> time];
[srcEntries addObject:srcEntry];
return SVN_NO_ERROR;
}
后面调用报错的地方(此处为一个tableview刷新时候的方法):
- (id) tableView: (NSTableView *)table
objectValueForTableColumn: (NSTableColumn *)column
row: (NSInteger)row {
SrcEntry *srcEntry = [self.srcEntries objectAtIndex:row];
return srcEntry.name;
}
最奇怪的是,我在刚开始转换完,调试看到的值和类型,和后面报错前看到的完全不一样。下面是两张调试变量时候的截图:
这张是报错前,调试变量截图:
注:就是在读取第二个元素的name属性的时候出错。这中间我并没有对数组里面的元素做任何改动,只是把数组拷贝到了另外一个地方。为什么类型和值都会发生变化呢?
追加:并且,从截图中可以看出属性path的值都丢掉了。变成了一串内存地址。难道我是需要用*符号,再取出实际的值吗?
追加2:第二张截图中,显示为(NSString *) xxxxx像是内存地址的name的属性。我试验了,调用任何方法都报错,并且像是死循环一样,断点一直反复进入那个地方出不来。请问有没有什么办法,绕过这个错?
What are the parameters of the @property (parameter 1, parameter 2) attribute of your srcEntry.name? Have you enabled ARC?
You said: I didn’t make any changes to the elements in the array, I just copied the array to another place.
e.g. The following is a scenario simulation
If you declare two properties like this:
and define
What is the output below?
The result is: String 2 = null
Since self.string1 and self.string2 point to the same address, and string2 does not retain the memory address, and self.string1=nil releases the memory, string1 is nil. For pointers declared as weak, once the address pointed to by the pointer is released, these pointers will be assigned a value of nil.
I think your name attribute is either weak or assign. It’s best to change it to copy