nsstring

How might I check if a particular NSString is present in an NSArray?

自作多情 提交于 2019-12-03 18:00:38
问题 How might I check if a particular NSString is presnet in an NSArray? 回答1: You can do it like, NSArray* yourArray = [NSArray arrayWithObjects: @"Str1", @"Str2", @"Str3", nil]; if ( [yourArray containsObject: yourStringToFind] ) { // do found } else { // do not found } 回答2: Iterating or containsObject are order n ways to find. If you want constant time lookup, you can also maintain a hash table like NSSet or NSHashTable but that increases space but saves time. NSArray* strings = [NSArray

AES256 NSString Encryption in iOS

孤街醉人 提交于 2019-12-03 17:55:32
问题 My app encrypts and decrypts (or it should) an NSString (the text to be encrypted / decrypted) with another NSString (the keyword) using aes 256-Bit Encryption. When I run my project and run the encrypt method, nothing gets encrypted the textfield just clears itself. Here is the code I have: -(void)EncryptText { //Declare Keyword and Text NSString *plainText = DataBox.text; NSString *keyword = Keyword.text; //Convert NSString to NSData NSData *plainData = [plainText dataUsingEncoding

Why does NSString sizeWithFont: return the wrong size?

你说的曾经没有我的故事 提交于 2019-12-03 17:07:56
I need to work out the height of a UITextView from the top down to the cursor. I am trimming the text it contains (so it only goes up to the cursor) and then using NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to work out the height, like so: NSRange range; range.location = noteTextView.selectedRange.location; range.length = noteTextView.text.length - range.location; NSString *string = [noteTextView.text stringByReplacingCharactersInRange:range withString:@""]; CGSize size = [string sizeWithFont:noteTextView.font constrainedToSize:noteTextView.frame.size lineBreakMode

casting NSInteger into NSString

房东的猫 提交于 2019-12-03 16:29:44
问题 I'm trying to make one string out of several different parts. This below is what i have right now. I don't get any errors in my code but as soon as i run this and do the event that triggers it i get EXC_BAD_ACCESS . Is there another way of casting this NSInteger into a NSString ? NSString *part1, *part2, *tempString; NSInteger num1; NSInteger num2; part1=@"some"; part2=@"text"; tempString = [NSString stringWithFormat:@"%@%@%@%@", part1, (NSString *)num1, part2, (NSString *)num2]; 回答1: A

How to find the text in an NSTextView?

我是研究僧i 提交于 2019-12-03 16:26:55
问题 I need to find the text in an NSTextView , and save it to a file. I can do the saving fine. I have used -stringValue , and -textStorage so far, but neither have worked. When I put -stringValue in, it just gave me (null), and when I put -textStorage in, it gave me a very large chunk of nothing (like a long paragraph of invisible text). How can I put the text from an NSTextView into an NSString ? 回答1: Try NSString *text = [[myTextView textStorage] string]; The NSTextStorage inherits from

What is the length in bytes of a NSString?

我的未来我决定 提交于 2019-12-03 16:22:18
问题 How do I get the bytes length of NSString ? if myString contains "hallo", myString.length will return 5, but how many actual bytes are taken? 回答1: NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%i bytes", bytes); 回答2: To get the bytes use NSData *bytes = [string dataUsingEncoding:NSUTF8StringEncoding]; Then you can check bytes.length Number of bytes depend on the string encoding 回答3: Well: NSString* string= @

“contentsOfFile” returning nil, possible cause

≡放荡痞女 提交于 2019-12-03 15:48:52
The following returns nil when getting the content of a csv file. However, reducing the csv table to 10 rows will work properly, outputting the content of the csv file. The original csv has about 400,000 characters arranged in 500 rows and 11 columns. What could be causing it to return nil with the original csv? let dbPath = "/Volumes/Gios2TWD/myDB.csv" var error: NSError? let csvContent = NSString(contentsOfFile: dbPath, encoding:NSUTF8StringEncoding, error: &error) as String! println(csvContent) println(error) I'm running XCode Version 6.1 (6A1030) error: Optional(Error Domain

How to check if an NSString contains one of the NSStrings in an NSArray?

坚强是说给别人听的谎言 提交于 2019-12-03 15:35:34
问题 I'm making an iOS app and I need to figure out if an NSString contains any of the NSStrings in an NSArray . 回答1: BOOL found=NO; for (NSString *s in arrayOfStrings) { if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) { found = YES; break; } } 回答2: It may be a silly optimization for your use case, but depending on how large the array is that you are iterating, it may be helpful/more performant to use NSArray's indexOfObjectWithOptions:passingTest: method. With this method you

UITextField get currently edited word

旧时模样 提交于 2019-12-03 15:35:32
I'm working on an autocompletion component and I have one problem that I would like to solve in some easy way. I want to support edits to autocompleted text, for example: blablabl @usertag blablabl If user goes back and edits @usertag string, I would like to start autocompletion when it's edited. Question is, how to get currently edited word from textfield. I thought about taking cursor position, seperate nsstring to word by " " (space) and count letters from first word to cursor position. Is there any easier way for doing that? Ok I found a way to do it quite easily. Maybe someone will find

How can I make unicode characters from integers?

我是研究僧i 提交于 2019-12-03 15:27:04
I want to make an array of Unicode characters, but I don't know how to convert integers into a Unicode representation. Here's the code I have so far NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0]; int i; for (i = 32; i < 300; i++) { NSString *uniString = [NSString stringWithFormat:@"\u%04X", i]; [uniArray addObject:uniString]; } Which gives me an error "incomplete universal character name \u" Is there a better way to build an array of Unicode symbols? Thanks. You should use %C to insert a unicode character: NSMutableArray *uniArray = [[NSMutableArray alloc]