gpt4 book ai didi

ios - NSString isEqualToString 不工作

转载 作者:可可西里 更新时间:2023-11-01 03:54:07 25 4
gpt4 key购买 nike

类定义如下:

@interface KNBookmark : NSObject
@property (nonatomic, strong) NSString *bookmarkId ;
@property (nonatomic, strong) NSString *novelName ;
@property (nonatomic, strong) NSString *novelId ;
@property (nonatomic, strong) NSString *novelPic ;
@property (nonatomic, strong) NSString *articleId ;
@property (nonatomic, strong) NSString *articleTitle ;
@property (nonatomic, strong) NSNumber *readRate ;
@end

@interface KNArticle : NSObject

@property (nonatomic, strong) NSString *num ;
@property (nonatomic, strong) NSString *articleId ;
@property (nonatomic, strong) NSString *title ;
@property (nonatomic, strong) NSString *subject ;
@property (nonatomic, strong) NSString *text ;
@end

源代码:

for(int i=0;i<count;i++)
{
KNArticle* article=[self.novel.articles objectAtIndex:i];

NSLog(@"bookmark articleId='%@' articleId='%@'",self.bookmark.articleId,article.articleId);

NSLog(@"bookmark articleId class='%@' articleId class='%@'",[self.bookmark.articleId class],[article.articleId class]);

if([self.bookmark.articleId isEqualToString:article.articleId])
{
NSLog(@"equal");
self.indexArticles=i;
break;
}
else
NSLog(@"not equal");
}

NSLog输出如下:

2014-07-16 22:56:14.768 novel[12685:90b] bookmark articleId='5680285' articleId='5680285'
2014-07-16 22:56:14.768 novel[12685:90b] bookmark articleId class='__NSCFString' articleId class='__NSCFNumber'
2014-07-16 22:56:14.769 novel[12685:90b] not equal

为什么“5680295”不等于“5680295”?我尝试了以下代码:

NSString* id1=@"5680295";
NSString* id2=@"5680295";
if([id1 isEqualToString:id2])
{
NSLog(@"equal");
}

上面的代码显示了“相等”,但我还是不知道为什么?

最佳答案

正如其他人所指出的,isEqualToString: 返回NO 的原因可能是因为article.articleId 不是字符串。

self.bookmark.articleId 必须是 NSString 的实例,因为它响应 isEqualToString:

确保 articleId 是预期的类型:

NSAssert([article.articleId isKindOfClass:[NSString class]], @"bad type in articleId");

关于ios - NSString isEqualToString 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781008/

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