【问题标题】:Can't set cell title's text无法设置单元格标题的文本
【发布时间】:2014-11-06 15:55:47
【问题描述】:

我正在编写简单的笔记应用程序,但是当我尝试将标题分配给 note.title 时出现错误。

-(Notez*)createNoteWithTitle:(NSString*)titleNote andText:(NSString*)textNote
{
    Notez *newNote  = [Notez new];
    if (!_notesArray) {
        _notesArray = [NSMutableArray new];
    }
    newNote.dateCreated = [NSDate new];
    newNote.title = [NSString stringWithFormat:@"23"];
    newNote.text = [NSString stringWithFormat:@"233"];
    [_notesArray addObject:newNote];
    return newNote;
    }

Notez.h: #导入

 @interface Notez : NSObject
 @property NSString *text;
 @property NSString *title;
 @property NSDate* dateCreated;
 -(Notez*)createNoteWithTitle:(NSString*)title andText:(NSString*)text;
-(void)save;
+(instancetype)sharedManager;
-(NSArray*)sortedNotes;
-(void)removeNoteAtIndex:(NSUInteger)index;
@end

note.dateCreated 可以,但是 note.title 和 note.text 不行。

它们都是 NSString...

- (IBAction)addNote:(id)sender {
    [[Notez sharedManager] createNoteWithTitle:@"Note Title" andText:@"Note Text"];
}

【问题讨论】:

  • 并且不要在 cmets 中添加更多细节。使用附加代码和信息更新您的问题。
  • @Greg 不,只需使用= @"23";
  • 你到底有什么错误?
  • 可能不是这个评论的地方,但是我觉得这个类的结构真的很不常规。您应该考虑将createNoteWithTitle:andText: 变成类方法而不是实例方法。就目前而言,您可以在任何实例上调用它(不仅仅是您的 sharedManager .. 顺便说一句,它根本不是经理)。
  • 有些东西你没有给我们看。

标签: ios objective-c nsstring


【解决方案1】:

由于您将titletext 声明为属性,但由于某种原因得到了异常: [Notez setTitle:]: 无法识别的选择器发送到实例,显然

我只能在这里做另一个猜测。通常,在声明一个属性时,您会得到一个 setter 和一个 getter 方法。这样,如果您的类上有很多实例变量,您可以省略手动编写这些。

使用dot notation 就相当于调用settergetter。所以在你的情况下

newNote.title = [NSString stringWithFormat:@"23"];

相当于:

[newNote setTitle:[NSString stringWithFormat:@"23"]];

现在,您建议 setter 方法的例外:setTitle: 实际上在您的对象上不存在。我不确定这可能是什么原因,但您可以尝试显式合成您的属性。

因此,在您的 .m 文件 中添加以下代码行:

@synthesize title;
@synthesize text;

不确定这是否真的能解决你的问题,但我希望propertiesgetterssetters 的解释能帮助你更多地了解发生了什么。

【讨论】:

  • 是的,我得到的是动态而不是合成。
  • @GeorgeSabanov 你不想要@dynamic@synthesize
猜你喜欢
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2015-12-16
  • 2012-01-10
  • 1970-01-01
相关资源
最近更新 更多