使用以下方法很容易将对象的类/地址作为字符串获取:
NSString* objectInfoString = [object description];
Run Code Online (Sandbox Code Playgroud)
返回的字符串是这样的<ClassName: fk10009567>,字母和数字代表它在内存中的唯一地址,据我所知.
或者,对于地址,只需使用带有%p占位符的格式化NSString.
NSString* addressString = [NSString stringWithFormat:@"%p",myObject];
Run Code Online (Sandbox Code Playgroud)
那么可以从这个字符串信息创建一个指针吗?例如:
NSObject* foo = [[NSObject alloc]init];
NSString* fooAddressString = [NSString stringWithFormat:@"%p",foo];
Run Code Online (Sandbox Code Playgroud)
然后,我希望能够做到这样的事情:
NSObject* newFoo = [NSObject objectForAddressString:fooAddressString];
Run Code Online (Sandbox Code Playgroud)
这样newFoo = foo就是真的.
我试图使用以下代码从iOS上的Xcode中的支持文件文件夹中访问.txt文件:
NSString* filePath = @"filename.txt";
NSLog(@"File path: %@", filePath);
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];
NSLog(@"File root: %@", fileRoot);
Run Code Online (Sandbox Code Playgroud)
第一个NSLog打印出我希望它打印的内容,但最后一个NSLog总是打印
文件根:(null)
在(尝试)将文件读入内存后从文件中访问文本也简单地给了我一个(null)打印输出.
这个问题的解决方案可能就在我的鼻子底下,我似乎无法找到它.任何帮助非常感谢!提前致谢.
通常在lldb调试器中,如果对对象执行po操作,则其地址也会与描述一起显示.但是,如果是NSString,则不显示地址.所以问题是有没有办法看到它.
PS - 源代码中的NSLog%p不是我要找的答案.
我有一个NSString的length4按道理,这应该工作,并且expYear做,而是expMonth抛出一个越界异常?
expYear = [expDate substringWithRange:NSMakeRange(0, 2)];
expMonth = [expDate substringWithRange:NSMakeRange(2, 3)];
Run Code Online (Sandbox Code Playgroud) 如何检查包含特定字符或单词的字符串.在我的情况下,我有一个字符串" 红色手册 ".在这里,我必须在我的字符串中检查"red ma".我通过使用范围的字符串方法尝试了它,但它不满足条件.
这是我的代码
NSString *string = @"red manual";
NSRange newlineRange = [string rangeOfString:@"red ma"];
if(newlineRange.location != NSNotFound)
{
NSLog(@"found");
}
else
{
NSLog(@"not found");
}
Run Code Online (Sandbox Code Playgroud) 我想为表格视图分配标题标题,但我希望标题分成两行。第二行应具有比顶行小的字体。
我正在尝试使用NSMutableAttributedString,但是在方法中:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
但它返回NSString而不是NSMutableAttributedString。
有什么办法,我可以转换NSMutableAttributedString到NSString同时保持字符串格式化?
这是我的代码:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = @"";
title = @"Top Line \n";
if(_favouriteLocations.count == 0){
title = [title stringByAppendingString:@"Smaller font Bottom Line"];
}
NSMutableAttributedString *attString =[[NSMutableAttributedString alloc] initWithString:title];
NSRange range = [title rangeOfString:@"Smaller"];
[attString setAttributes:@{NSFontAttributeName:@"8"} range:NSMakeRange(range.location, title.length - range.location)];
NSLog(@"%@", attString);
title = [attString string];
return title;
}
Run Code Online (Sandbox Code Playgroud) 我NSTimeInterval从以前的各种计算中得到了几个NSDates.现在我想以days:hours:minutes:seconds格式向用户显示这些间隔.
在我的应用程序的早期,我使用此代码在稍微不同的上下文中显示信息:
-(void)updateDurationLabel
{
//Which calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//Gets the componentized interval from the most recent time an activity was tapped until now
NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:self.startTime toDate:[NSDate date] options:0];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds =[components second];
// Converts the components to a string and displays it in the duration label, updated via the timer
cellLabelTempText = [NSString …Run Code Online (Sandbox Code Playgroud) 有一个应用程序,我在其中显示用户在地址中的当前位置.问题是,如果例如邮政编码或管理区域不可用,则字符串将打印(null)该值应保留的位置 - 所有其他数据都在那里.
例:
(null)19号路
(null)孟买
马哈拉施特拉邦
我想知道的是,是否可以只有一个空格而不是(null)?
我目前的代码:
_addressLabel.text = [NSString stringWithFormat: @"%@ %@\n%@ %@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea];
Run Code Online (Sandbox Code Playgroud) 好吧,我以前做过这个,很容易,但由于某种原因我不明白为什么以下两个字符串不相等!有任何想法吗?我确信这将是一件非常容易的事情,我没有看到:
NSString *texture1name = [NSString stringWithFormat:@"world%d?nGameScrollBackground1", 0];
NSLog(@"IS EQUAL: %d", [@"world0InGameScrollBackground1" isEqualToString:texture1name]);
NSLog(@"This: %@ equal to: %@", texture1name, @"world0InGameScrollBackground1");
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到一个:
IS EQUAL: 0 This: world0?nGameScrollBackground1 equal to: world0InGameScrollBackground1
它完全相同的字符串,没有空格或任何东西!请帮忙,谢谢!
我试图从包含表情符号Unicode的后端提取JSON文件。这些不是旧版Unicode(例如:\ ue415),而是跨平台工作的Unicode(例如:\ U0001F604)。
这是被提取的json示例:
[
{
"unicode": "U0001F601",
"meaning": "Argh!"
},
{
"unicode": "U0001F602",
"meaning": "Laughing so hard"
}
]
Run Code Online (Sandbox Code Playgroud)
我很难将这些字符串转换为将在应用程序中显示为表情符号的unicode。
任何帮助是极大的赞赏!