说我有一个字符串:
NSString *state = @"California, CA";
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何从这个字符串中提取最后两个字符(在本例中为@"CA")
我正在开发一款iPhone应用程序.在标签中,我想显示用户名字大写的第一个字母.我怎么做?
在我的应用程序的iOS 5版本中,我有:
[self.text drawInRect: stringRect
withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize]
lineBreakMode: NSLineBreakByTruncatingTail
alignment: NSTextAlignmentRight];
Run Code Online (Sandbox Code Playgroud)
我正在升级iOS 7.上面的方法已被弃用.我现在正在使用drawInRect:withAttributes : . 的属性参数是一个NSDictionary对象.我可以得到drawInRect:withAttributes:使用这个来处理以前的字体参数:
UIFont *font = [UIFont fontWithName: @"Courier" size: kCellFontSize];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
nil];
[self.text drawInRect: stringRect
withAttributes: dictionary];
Run Code Online (Sandbox Code Playgroud)
我将哪些键值对添加到字典中以获取NSLineBreakByTruncatingTail和NSTextAlignmentRight?
我需要连接a String和Int如下:
let myVariable: Int = 8
return "first " + myVariable
Run Code Online (Sandbox Code Playgroud)
但它没有编译,错误:
二进制运算符'+'不能应用于'String'和'Int'类型的操作数
连接String + Int的正确方法是什么?
(已搜索过,但无法在此处或在Cocoa文档中找到这个的简单解决方案)
问:如何仅从NSString中修剪所有前导空格?(即保留其他任何空格.)
不幸的是,出于我的目的,NSString的stringByTrimmingCharactersInSet方法适用于前导和尾随.
需要Mac OS X 10.4兼容性,手动GC.
我想改变一个句子,例如:
pas pas pas pas re re.C'étaitlà-bas.
会成为:
Etre ou ne pas etre.C'etait la-bas.
使用NSString有什么简单的方法吗?或者我必须通过检查每个字符来自己开发它?
我正在构建一个iPhone应用程序,并坚持以下:
unsigned char hashedChars[32];
CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], hashedChars);
NSData *hashedData = [NSData dataWithBytes:hashedChars length:32];
NSLog(@"hashedData = %@", hashedData);
Run Code Online (Sandbox Code Playgroud)
日志显示如下:
hashedData = <abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh>
Run Code Online (Sandbox Code Playgroud)
但我需要的是将hashedData转换为NSString,如下所示:
NSString *someString = @"abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
Run Code Online (Sandbox Code Playgroud)
所以基本上结果需要像hashedData,除了我不想在它们之间使用尖括号和空格.
我正在使用非常棘手的战斗方法:)来制作一个像Fi?le*/ Name文件名一样安全的字符串File_Name.我敢肯定有一种可可的方式来转换它.我相信最好的地方就是:)
谢谢!
在Objective-C中,我的理解是指令@"foo"定义了一个常量NSString.如果我在多个地方使用@"foo",则引用相同的不可变NSString对象.
为什么我经常看到这段代码片段(例如在UITableViewCell重用中):
static NSString *CellId = @"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellId];
Run Code Online (Sandbox Code Playgroud)
而不仅仅是:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"CellId"];
Run Code Online (Sandbox Code Playgroud)
我假设它是为了保护我不会在编译器无法捕获的标识符名称中输入错误.但如果是这样,我不能只是:
#define kCellId @"CellId"
Run Code Online (Sandbox Code Playgroud)
并避免静态NSString*位?或者我错过了什么?
我NSURLRequest在我的iPhone应用程序中使用该类,并且调用它的方法返回一个NSString很好的连接通过正常,但问题是我需要将NSError转换为NSString所以我可以将其返回或if()对它进行一些陈述.
有任何想法吗?:)
nsstring ×10
objective-c ×5
ios ×3
cocoa ×2
iphone ×2
string ×2
swift ×2
cocoa-touch ×1
filenames ×1
ios7 ×1
nsdata ×1
nserror ×1
trim ×1
utf-8 ×1
whitespace ×1