标签: nsstring

objective-c获取字符串的最后2个字符?

说我有一个字符串:

NSString *state = @"California, CA";
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何从这个字符串中提取最后两个字符(在本例中为@"CA")

iphone objective-c nsstring

47
推荐指数
1
解决办法
4万
查看次数

如何在UILabel中制作首字母大写字母?

我正在开发一款iPhone应用程序.在标签中,我想显示用户名字大写的第一个字母.我怎么做?

cocoa-touch objective-c nsstring ios swift

47
推荐指数
5
解决办法
4万
查看次数

使用drawInRect:withAttributes对齐文本:

在我的应用程序的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)

我将哪些键值对添加到字典中以获取NSLineBreakByTruncatingTailNSTextAlignmentRight

nsstring ios ios7

46
推荐指数
2
解决办法
3万
查看次数

在Swift中用字符串连接数字

我需要连接a StringInt如下:

let myVariable: Int = 8
return "first " + myVariable
Run Code Online (Sandbox Code Playgroud)

但它没有编译,错误:

二进制运算符'+'不能应用于'String'和'Int'类型的操作数

连接String + Int的正确方法是什么?

string nsstring swift

46
推荐指数
4
解决办法
4万
查看次数

Cocoa - 修剪NSString中的所有前导空格

(已搜索过,但无法在此处或在Cocoa文档中找到这个的简单解决方案)

问:如何仅从NSString中修剪所有前导空格?(即保留其他任何空格.)

不幸的是,出于我的目的,NSString的stringByTrimmingCharactersInSet方法适用于前导和尾随.

需要Mac OS X 10.4兼容性,手动GC.

cocoa whitespace trim nsstring

44
推荐指数
5
解决办法
4万
查看次数

NSString:从字符串中删除UTF-8重音的简单方法?

我想改变一个句子,例如:

pas pas pas pas re re.C'étaitlà-bas.

会成为:

Etre ou ne pas etre.C'etait la-bas.

使用NSString有什么简单的方法吗?或者我必须通过检查每个字符来自己开发它?

objective-c utf-8 nsstring

44
推荐指数
6
解决办法
2万
查看次数

NSData显示为字符串

我正在构建一个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是NSData,而不是NSString

但我需要的是将hashedData转换为NSString,如下所示:

NSString *someString = @"abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
Run Code Online (Sandbox Code Playgroud)

所以基本上结果需要像hashedData,除了我不想在它们之间使用尖括号和空格.

iphone nsstring nsdata

43
推荐指数
2
解决办法
4万
查看次数

如何使NSString路径(文件名)安全

我正在使用非常棘手的战斗方法:)来制作一个像Fi?le*/ Name文件名一样安全的字符串File_Name.我敢肯定有一种可可的方式来转换它.我相信最好的地方就是:)

谢谢!

string cocoa filenames nsstring

42
推荐指数
3
解决办法
1万
查看次数

静态NSString用法与内联NSString常量

在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*位?或者我错过了什么?

objective-c nsstring ios

42
推荐指数
3
解决办法
2万
查看次数

从NSError返回NSString

NSURLRequest在我的iPhone应用程序中使用该类,并且调用它的方法返回一个NSString很好的连接通过正常,但问题是我需要将NSError转换为NSString所以我可以将其返回或if()对它进行一些陈述.

有任何想法吗?:)

objective-c nsstring nserror

42
推荐指数
4
解决办法
4万
查看次数