一些奇怪的事情正在发生。当我将 NSString 重新分配给靠近代码底部的 subString 变量时。输出中 subString 的值似乎为空。我不知道 objectAtIndex 是返回一些奇怪的东西还是内存问题。如果我创建一个新变量而不是重新分配 subString 的值,我可以在输出控制台中打印看到正确的值。如果有人可以帮我解决这个问题。将不胜感激。
NSString *subString = @"";
if ([text length] > 0)
{
UITextRange *selectedRange = [_textView selectedTextRange];
UITextPosition *cursorPosition = [_textView positionFromPosition:selectedRange.start offset:0];
UITextRange *subTextRange = [_textView textRangeFromPosition:_textView.beginningOfDocument toPosition:cursorPosition];
subString = [textView textInRange:subTextRange];
}
NSLog(subString);
NSLog(@" %s", [subString hasSuffix:@" "] ? "TRUE" : "FALSE");
BOOL hasSpaceSuffix = [subString hasSuffix:@" "];
NSLog(@" %s", _taggingInProgress ? "TRUE" : "FALSE");
NSArray *substringArray = [[subString componentsSeparatedByString:@" "] retain];
if ([substringArray count] > 1) {
int index = [substringArray count];
if ([[substringArray objectAtIndex:index-1] isEqualToString:@" "])
{
NSLog(@"1st");
subString = [substringArray objectAtIndex:index-2];
NSLog(subString);
}
else
{
NSLog(@"2nd");
subString = [substringArray objectAtIndex:index-1];
NSLog(subString);
}
NSLog(@"AFTER");
NSLog(subString);
}