nsstring

Replacing bad words in a string in Objective-C

戏子无情 提交于 2019-12-01 20:05:50
I have a game with a public highscore list where I allow layers to enter their name (or anything unto 12 characters). I am trying to create a couple of functions to filter out bad words from a list of bad words I have in a text file. I have two methods: One to read in the text file: -(void) getTheBadWordsAndSaveForLater { badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"]; badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil]; badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile]; badwords =

Why is NSString stringWithString returning pointer to copied string?

情到浓时终转凉″ 提交于 2019-12-01 19:44:12
I'm trying to copy an NSString value out of an NSMutableArray into a new variable. NSString stringWithString is returning an NSString with the same memory address as the object in my array. Why? #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *arr = [NSMutableArray arrayWithObject:@"first"]; NSLog(@"string is '%@' %p", [arr objectAtIndex:0], [arr objectAtIndex:0]); // copy the string NSString *copy = [NSString stringWithString:[arr objectAtIndex:0]]; NSLog(@"string is '%@' %p", copy, copy); } return 0; } 1) Whenever you're creating

深入理解iPhone数据持久化

泄露秘密 提交于 2019-12-01 19:18:51
在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中。symbian中因为权限认证的原因,在3rd上大多数只能访问应用程序的private目录或其它系统共享目录。在iphone中,apple博采众长,提供了多种数据持久化的方法,下面笔者会逐个进行详细的讲解。 iphone提供的数据持久化的方法,从数据保存的方式上讲可以分为三大部分:属性列表、对象归档、嵌入式数据库(SQLite3)、其他方法。 一、属性列表NSUserDefaults NSUserDefaults类的使用和NSKeyedArchiver有很多类似之处,但是查看NSUserDefaults的定义可以看出,NSUserDefaults直接继承自NSObject而NSKeyedArchiver 继承自NSCoder。这意味着NSKeyedArchiver实际上是个归档持久化的类,也就可以使用NSCoder类的[encodeObject: (id)objv forKey:(NSString *)key]方法来对数据进行持久化存储。 - (void)applicationDidFinishLaunching:(UIApplication *)application { NSString *strOne = @"Persistent

How to use NULL character with NSString?

空扰寡人 提交于 2019-12-01 19:12:08
In PHP, I can call base64_encode("\x00". $username. "\x00". $password) and the "\x00" represents a NULL character. Now, in Objective-C, I have a function that converts NSData to base64 encoded NSString created by DaveDribin. How do I create data from a string that has NULL characters? This doesn't seem to work... NSData * authCode = [[NSString stringWithFormat:@"%c%@%c%@", '\0', self.username, '\0', self.password] dataUsingEncoding:NSUTF8StringEncoding]; stefanB Like this: char bytes[] = "\0username\0password"; NSData * data = [NSData dataWithBytes:bytes length:sizeof(bytes)]; NSLog(@"%@",

Detect whole word in NSStrings

穿精又带淫゛_ 提交于 2019-12-01 18:29:39
How do I detect if an NSString contains a specific word, e.g. is . If the NSString is Here is my string. His isn't a mississippi isthmus. It is... ? The method should detect the word is and return YES . However, if the NSString is His isn't a mississipi isthmus , it should return NO . I tried using if ([text rangeOfString:@"is" options:NSCaseInsensitiveSearch].location != NSNotFound) { ... } but it detects characters not words. Use "regular expression" search with the "word boundary pattern" \b : NSString *text = @"Here is my string. His isn't a mississippi isthmus. It is..."; NSString

UIImage to byte array

馋奶兔 提交于 2019-12-01 18:19:35
I'm creating an app that uploads an image to a server. It must send the byte array on a XML. How do I get the byte array into a NSString? Thanks! Suhail Patel You can convert the UIImage to a NSData object and then extract the byte array from there. Here is some sample code: UIImage *image = [UIImage imageNamed:@"image.png"]; NSString *byteArray = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; If you are using a PNG Image you can use the UIImagePNGRepresentation function as shown above or if you are using a JPEG Image, you can use

Separate 1 NSString into two NSStrings by whiteSpace

我的未来我决定 提交于 2019-12-01 18:12:00
I have an NSString which initially looked like <a href="http://link.com"> LinkName</a> . I removed the html tags and now have an NSString that looks like http://Link.com SiteName how can I separate the two into different NSString s so I would have http://Link.com and SiteName I specifically want to show the SiteName in a label and just use the http://Link.com to open in a UIWebView but I can't when it is all one string. Any suggestions or help is greatly appreciated. NSString *s = @"http://Link.com SiteName"; NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet

iOS开发之常用的那些工具类和方法

百般思念 提交于 2019-12-01 17:57:25
LBUtils: iOS开发常用工具类 NSDateUtil.h ----日期相关的工具类 功能: 指定日期格式的转换 NSFileUtil.h ----文件目录相关的工具类 功能: 获取Documents的路径 获取Cache的路径 判断文件是否存在 根据文件路径删除文件 NSString+Wrapper.h ----针对NSString的一些封装 功能: 验证字符串是否为空 获取字符串长度,区分中英文 移除字符串中的所有空字符 ...... 详细了解请前往GitHub: https://github.com/mangooc/LBUtils.git 希望对您有所帮助,欢迎开发者补充和完善,谢谢! 来源: oschina 链接: https://my.oschina.net/u/1440723/blog/657241

Objective-C Convert NSData to NSString

眉间皱痕 提交于 2019-12-01 17:52:36
问题 Here are the code I tried to convert NSData to NSString but the program return "Program received signal:SIGABRT". NSString *string= [NSString stringWithUTF8String:[data bytes]]; OR NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; Is there any other better way to do it? 回答1: Here's a highly up-voted answer that shows how to do it. In a nutshell: NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; The first idea looks like

Objective-C Convert NSData to NSString

[亡魂溺海] 提交于 2019-12-01 17:41:48
Here are the code I tried to convert NSData to NSString but the program return "Program received signal:SIGABRT". NSString *string= [NSString stringWithUTF8String:[data bytes]]; OR NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; Is there any other better way to do it? danh Here's a highly up-voted answer that shows how to do it. In a nutshell: NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; The first idea looks like a fail because of sending [data bytes] . stringWithUTF8String isn't prepared for a void *. The second