标签: nsstring

如何从像@"xxx =%@,yyy =%@"和NSArray对象这样的格式字符串创建NSString?

有没有办法从像@"xxx =%@,yyy =%@"和NSArray对象这样的格式字符串创建一个新的NSString?

在NSSTring类中有许多方法,如:

- (id)initWithFormat:(NSString *)format arguments:(va_list)argList
- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList
+ (id)stringWithFormat:(NSString *)format, ...
Run Code Online (Sandbox Code Playgroud)

但是没有它们将NSArray作为参数,我找不到从NSArray创建va_list的方法......

cocoa objective-c string-formatting nsstring nsarray

30
推荐指数
3
解决办法
5万
查看次数

"复制"和"保留"有什么区别?

是什么区别copy,并retainNSString

- (void)setString:(NSString*)newString
{
    string = [newString copy];
}
Run Code Online (Sandbox Code Playgroud)

copy objective-c nsstring retain

30
推荐指数
5
解决办法
3万
查看次数

如何在不插入空格的情况下替换NSString中的字符?

我们假设我有字符串

NSString* myString  = @"Hello,";
Run Code Online (Sandbox Code Playgroud)

如何在不留空格的情况下删除逗号?我试过了:

NSString* newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
Run Code Online (Sandbox Code Playgroud)

NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
Run Code Online (Sandbox Code Playgroud)

但两者都留下了空间.

iphone cocoa cocoa-touch objective-c nsstring

30
推荐指数
1
解决办法
5万
查看次数

如何检查NSString是否包含数值?

我有一个从公式生成的字符串,但是我只想使用该字符串,只要它的所有字符都是数字,如果不是我想做不同的事情,例如显示错误信息.

我一直在看,但我发现很难找到任何符合我想做的事情.我看过NSScanner,但我不确定它是否检查整个字符串然后我不确定如何检查这些字符是否是数字

- (void)isNumeric:(NSString *)code{

    NSScanner *ns = [NSScanner scannerWithString:code];
    if ( [ns scanFloat:NULL] ) //what can I use instead of NULL?
    {
        NSLog(@"INSIDE IF");
    }
    else {
    NSLog(@"OUTSIDE IF");
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,经过几个小时的搜索,我偶然发现了一个完全符合我要求的实现.

因此,如果您要查看它们是否是您的NSString中的任何字母数字字符,则此处可用

-(bool) isNumeric:(NSString*) hexText
{

    NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];

    NSNumber* number = [numberFormatter numberFromString:hexText];

    if (number != nil) {
        NSLog(@"%@ is numeric", hexText);
        //do some stuff here      
        return true;
    }

    NSLog(@"%@ is not numeric", hexText);
    //or do some more stuff here
    return false;
} …
Run Code Online (Sandbox Code Playgroud)

iphone numeric nsstring ios

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

将int转换为NSString

我想转换一个随机的int数字NSString然后将NSString分配给另一个,NSString但它崩溃了应用程序

我正在做以下事情

int mynumber =(arc4random() % 1000 );

unique = [NSString stringWithFormat:@"%d",mynumber];

   NSLog(unique)

   NSString*test=unique;
Run Code Online (Sandbox Code Playgroud)

当我写最后一行时它会崩溃;

它还会在我输入unique字符串时打印值.

iphone int nsstring

30
推荐指数
2
解决办法
10万
查看次数

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

我正在开发我的应用程序的新版本,并且我正在尝试替换已弃用的消息,但我无法通过这个消息.

我无法弄清楚为什么drawInRect:withAttributes不起作用.drawInRect:withFont:lineBreakMode:alignment发送消息时代码正确显示,但drawInRect:withAttributes发送时不起作用.

我使用相同的矩形和字体,我相信是相同的文本样式.常量只是将rect定位在图像下方,但我对两个调用使用相同的rect,所以我确定矩形是正确的.

(注意下面使用的bs.name是一个NSString对象)

        CGRect textRect = CGRectMake(fCol*kRVCiPadAlbumColumnWidth,
                                     kRVCiPadAlbumColumnWidth-kRVCiPadTextLabelYOffset,
                                     kRVCiPadAlbumColumnWidth,
                                     kRVCiPadTextLabelHeight);
        NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        textStyle.lineBreakMode = NSLineBreakByWordWrapping;
        textStyle.alignment = NSTextAlignmentCenter;
        UIFont *textFont = [UIFont systemFontOfSize:16];
Run Code Online (Sandbox Code Playgroud)

使用上面的变量,这不起作用(屏幕上没有任何内容)

        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];
Run Code Online (Sandbox Code Playgroud)

这可以使用上面相同的变量工作(在屏幕上正确绘制字符串)

        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];
Run Code Online (Sandbox Code Playgroud)

任何援助都会很棒.谢谢.

objective-c nsstring ios drawinrect

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

如何知道iOS中的UITextField是否有空格

我有一个UITextField,用户可以在其中输入名称并保存.但是,不应允许用户在textFiled中输入空格.

1 - 如何找出用户是否在textFiled中输入了两个空格或填写空格

2 - 我怎么知道textFiled是否只填充空格

编辑 - 仅输入空格(空格)无效

iphone nsstring uitextfield ipad ios

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

删除前缀objective-c mac

如何从NSString中删除前缀"test"?我试过stringByReplacingOccurrencesOfString:但它不是我想要的,因为它是我想要删除的前缀,而不是从字符串的其他出现.

string macos cocoa objective-c nsstring

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

在iOS 7中NSLineBreakMode的等效字符串绘制方法是什么?

有一种方法

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
Run Code Online (Sandbox Code Playgroud)

我现在必须替换iOS 7.我得到了

NSDictionary *attributes = @{NSFontAttributeName: font};
[self drawInRect:rect withAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)

但如何指定像换行一样的换行模式?我搜索了归因字符串绘制符号的文档但没有提到换行模式.这自动化总是自动换行吗?

nsstring uikit nsattributedstring ios

29
推荐指数
2
解决办法
1万
查看次数

在Swift中调用String上的NSString方法

Apple的Swift 文档说明了这一点

如果您在Cocoa或Cocoa Touch中使用Foundation框架,则可以使用整个NSString API调用您创建的任何String值

如果我有一个String对象,例如

var newString: String = "this is a string"
Run Code Online (Sandbox Code Playgroud)

如何containsString在我的String var上执行NSString操作?

nsstring swift

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