新手问题:NSOperation for iphone SDK

发布于 2024-08-22 04:48:53 字数 788 浏览 1 评论 0原文

您好,我在 NSOperation 方面遇到了一些问题。 我总是在 self = [super init]; 处遇到错误(已经使用断点来找到它) 它总是返回“程序收到信号:EXC_BAD_ACCESS”,

//AddThread.h
@interface AddThread : NSOperation
{
    NSString * str;
}
@property (nonatomic,retain) NSString * str;
-(id) initWithString:(NSString *) tmpStr;
@end

并且一直以来

//AddThread.m
#import "AddThread.h"
@implementation AddThread
@synthesize str;
- (id) initWithString:(NSString *)tmpStr
{
    self = [super init];
    if (self != nil)
    {
        self.str = tmpStr;
    }
    //NSLog(self);
    //[super init];
        return self;
}
- (void) main
{
    NSLog(self.str);
}
- (void) dealloc{
    [str release];
    str = nil;
    [super dealloc];
}
@end

,我一直坚持这一点,如果可能的话,有任何资源、文章来了解 NS 操作的基本示例吗?

Hi I got some problem with NSOperation .
I always got error at self = [super init]; (already use break point to find this)
it always return "Program received signal: EXC_BAD_ACCESS" all the time

//AddThread.h
@interface AddThread : NSOperation
{
    NSString * str;
}
@property (nonatomic,retain) NSString * str;
-(id) initWithString:(NSString *) tmpStr;
@end

and for .m

//AddThread.m
#import "AddThread.h"
@implementation AddThread
@synthesize str;
- (id) initWithString:(NSString *)tmpStr
{
    self = [super init];
    if (self != nil)
    {
        self.str = tmpStr;
    }
    //NSLog(self);
    //[super init];
        return self;
}
- (void) main
{
    NSLog(self.str);
}
- (void) dealloc{
    [str release];
    str = nil;
    [super dealloc];
}
@end

well I stuck with this for while and if possible any resources ,articles things for basic example of NSoperation?

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

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

发布评论

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

评论(1

太阳男子 2024-08-29 04:48:53

在您的 main 方法中,您正在调用 NSLog(self.str) - 如果您传入的对象是字符串,则这将起作用,但如果您继续尝试记录其他对象,则它将不起作用。 NSLog 采用格式字符串作为参数。如果您只是像在某些注释代码中那样执行 NSLog(self) ,并且 self 不是字符串,那么它会崩溃,因为它需要一个字符串。您应该执行 NSLog(@"self: %@", self) %@ 将打印出对象 description 方法返回的字符串。

除此之外,您的 init 方法看起来不错,您到底是如何创建该对象的实例的?你能展示一下代码吗?问题可能就出在这里。

In your main method, you are calling NSLog(self.str) - While this will work if the object you pass in is a string, it won't work if you continue to try and log other objects. NSLog takes a format string as a parameter. If you just do NSLog(self) like you are in some of your commented code, and self is not a string, it'll crash because it expected a string. You should do NSLog(@"self: %@", self) the %@ will print out the string returned by an objects description method.

Other than that, your init method looks fine, how exactly are you creating an instance of this object? Could you show the code for that? The problem may lie there.

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