Objective C - 在 vi​​ewDidLoad 中设置变量并在其他地方使用它

发布于 2024-11-15 16:09:27 字数 1037 浏览 3 评论 0 原文

另一个客观的c问题给你。可能也是一个简单的...

在我的 viewDidLoad 方法中,我正在设置一个变量。我需要在 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 中访问它,但是我收到以下错误,但我没有真的知道排序的方法!

-[CFString intValue]:发送到已释放实例 0x592dcb0 的消息

- (void)viewDidLoad
{   
    //code to get json is here
    totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    [super viewDidLoad];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)celllforRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    if((indexPath.row == [myArray count]-1)&&(indexPath.row < [totalInCategory intValue]))
    {
        //code to get more rows
        [self.tableView reloadData];
    }
}

我有 NSString *totalInCategory;建立并综合。

TotalInCategory 在 vi​​ewDidLoad 中很好(据我所知),但在其他地方......任何想法都会非常方便!

another objective c question for you. Probably a simple one too...

In my viewDidLoad method I am setting a variable. I need to access this within - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath however I am receivig the following error and I don't really know a way to sort it!

-[CFString intValue]: message sent to deallocated instance 0x592dcb0

- (void)viewDidLoad
{   
    //code to get json is here
    totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    [super viewDidLoad];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)celllforRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    if((indexPath.row == [myArray count]-1)&&(indexPath.row < [totalInCategory intValue]))
    {
        //code to get more rows
        [self.tableView reloadData];
    }
}

I have NSString *totalInCategory; set up and synthesized.

totalInCategory is fine within viewDidLoad (as far as I'm aware that is), but nowhere else... any ideas would be really handy!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海夜未眠 2024-11-22 16:09:27

保留数据...

totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

objectForKey 返回一个自动释放的对象,如果您想使用它,您需要保留该对象。不要忘记在 dealloc 中添加 [totalInCategory release];

Retain the data...

totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

objectForKey returns an autoreleased object which you need to retain if you wish to use it. Don't forget to add a [totalInCategory release]; in the dealloc.

友欢 2024-11-22 16:09:27

put:

[totalInCategory keep] 就在 totalInCategory = [parsedJson objectForKey:@"TotalInCategory"]; 下面,

这将保留当前由 objectForKey 自动释放的变量方法。

put:

[totalInCategory retain] just below totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];

That will retain the variable, which is currently autoreleased by the objectForKey method.

江南月 2024-11-22 16:09:27

在不知道 parsedJson 类型的情况下,我无法确定,但您似乎缺少保留。

 totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

在类的 dealloc 方法中添加

 [totalInCategory release];

您可能还应该看一下 iOS 内存管理编程指南(如果您还没有)。

Without knowing the type of parsedJson I can't be sure, but it seems likely that you're missing a retain.

 totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

And in the dealloc method for the class add

 [totalInCategory release];

You should probably also take a glance at the iOS Memory Management Programming Guide if you haven't already.

很酷又爱笑 2024-11-22 16:09:27

totalInCategoryself 结合使用。如果您在属性声明中指定了retain

self.totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];

Use totalInCategory with self. if you have specified retain in the property declaration .

self.totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文