gpt4 book ai didi

iphone - 我确信变量范围存在问题

转载 作者:行者123 更新时间:2023-11-29 04:57:43 25 4
gpt4 key购买 nike

我基本上遇到了变量范围的问题。了解多种编程语言,有时我就是无法理解 obj-c。

在我的 webviewcontroller 类中,我有一个使用 self.var 调用的变量

我还有一些引用修改后的 UIAlertView 的委托(delegate)方法。为什么我不能在这些方法中引用这个 self.var 而不会导致整个应用程序崩溃?

我已经尝试了所有方法,已经过去几天了,现在我需要帮助!

谢谢!

编辑:

这是代码webviewcontroller.h

    #import "SBTableAlert.h"

@interface WebViewController : UIViewController <SBTableAlertDelegate, SBTableAlertDataSource>{
NSMutableArray *bookmarks;
}
@property (nonatomic, retain) NSMutableArray *bookmarks;

这里是 .m(是的,我有一个用于书签的@synthesize),这是在 View 中加载的

    NSMutableArray *tmpArray = [tmpDict objectForKey:@"myKey"];
self.bookmarks = tmpArray;
[tmpDict release];
[tmpArray release];

SBTableAlert *bookmarkAlert;

bookmarkAlert = [[SBTableAlert alloc] initWithTitle:@"Jump to:" cancelButtonTitle:@"Back" messageFormat:nil];
[bookmarkAlert.view setTag:1];
[bookmarkAlert setDelegate:self];
[bookmarkAlert setDataSource:self];

NSString *settingsicon = [[NSBundle mainBundle] pathForResource:@"gear" ofType:@"png"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:settingsicon] style:UIBarButtonItemStylePlain target:bookmarkAlert action:@selector(show)];

后来我尝试调用 self.bookmarks

    - (NSInteger)tableAlert:(SBTableAlert *)tableAlert numberOfRowsInSection:(NSInteger)section {
return [self.bookmarks count];
}

我在 self.bookmarks 计数中遇到的错误是 NSInvalidArgumentException

最佳答案

如果没有看到您的一些代码,就很难回答。

就像 Farmer_Joe 所问的那样,您如何定义该属性?像这样的东西:

@property (nonatomic, retain) MyVar *var;

不同于

@property MyVar *var;

你如何分配给var?

var = value;

不会启动保留,但是这会(如果该属性被定义为“保留”):

self.var = value

编辑:根据您发布的新代码,您似乎过度释放了 tmpArray。删除这一行:

[tmpArray release];

您不必释放 tmpArray,因为这一行不会保留它:

NSMutableArray *tmpArray = [tmpDict objectForKey:@"myKey"];

关于iphone - 我确信变量范围存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7612144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com