nsstring

Getting the substring from a certain character in NSString

白昼怎懂夜的黑 提交于 2019-12-03 14:35:39
问题 If I have an NSString that is initially: "ABCDE*FGHI" How do I make it turn into "FGHI" In other words, everything from the asterisk onwards is kept. Likewise, how would I turn it into: "ABCDE" (everything up to the asterisk is kept) Thanks 回答1: NSString *myString = @"ABCDE*FGHI"; NSArray *myArray = [myString componentsSeparatedByString:@"*"]; key 0 of myArray will contain @"ABCDE" key 1 will contain @"FGHI" If you want more than one character to be the separator, use

Shuffling Letters in an NSString in Objective-C

我的未来我决定 提交于 2019-12-03 14:17:21
I have written this function which shuffles the contents of a NSString , and it seems to work, but every now and then it crashes. This may be a roundabout way, but I put the characters into an array, swap the elements in the array randomly, and then turn the array back into a string. I'm not sure what I am doing that is unsafe which makes it crash. I thought it was possibly that I am setting finalLettersString = result , but I also tried finalLettersString = [NSString stringWithString:result] and that also crashes. The reason I am confused is because it does not crash every time. I just keep

Global NSString

我只是一个虾纸丫 提交于 2019-12-03 14:12:06
I need to create an NSString, so I can set its value in one class and get it in another. How can I do it? if you write: NSString *globalString = @"someString"; anywhere outside a method, class definition, function, etc... it will be able to be referenced anywhere. (it is global!) The file that accesses it will declare it as external extern NSString *globalString; This declaration signifies that it is being accessed from another file. Make it a global variable. In one file in global scope: NSMutableString *myString = @"some funny string"; In the other file: extern NSMutableString *myString;

How to convert an NSString to an unsigned int in Cocoa?

风格不统一 提交于 2019-12-03 14:00:27
My application gets handed an NSString containing an unsigned int . NSString doesn't have an [myString unsignedIntegerValue]; method. I'd like to be able to take the value out of the string without mangling it, and then place it inside an NSNumber . I'm trying to do it like so: NSString *myUnsignedIntString = [self someMethodReturningAString]; NSInteger myInteger = [myUnsignedIntString integerValue]; NSNumber *myNSNumber = [NSNumber numberWithInteger:myInteger]; // ...put |myNumber| in an NSDictionary, time passes, pull it out later on... unsigned int myUnsignedInt = [myNSNumber

NSData from NSKeyedArchiver to NSString

人走茶凉 提交于 2019-12-03 13:50:28
I'm trying to convert NSData generated from NSKeyedArchiver to an NSString so that I can pass it around and eventually convert it back to NSData. I have to pass this as a string (I'm using three20 URL passing). I've gone through various encodings, UTF8, ASCII, etc. and can't get anything to work. NSKeyedArchiver says that the NSData is formated as a property list: NSPropertyListBinaryFormat_v1_0. Does anyone have any idea how I can convert this NSData to a String and back again? Size of the string isn't an issue. Thanks What you want is: id<nscoding> obj; NSData * data = [NSKeyedArchiver

iOS杂记-告警清理

你。 提交于 2019-12-03 13:41:34
NS_ASSUME_NONNULL_BEGIN @interface Robot : NSObject @property (copy,readonly) NSString *name; - (nullable instancetype)initWithName:(NSString *)name; - (nullable NSString *)tellMeSomething; @end NS_ASSUME_NONNULL_END 来源: https://www.cnblogs.com/zhang-pengcheng/p/11797627.html

How to read text file without knowing the encoding

元气小坏坏 提交于 2019-12-03 13:30:06
When reading a text file that was created somewhere else outside my app, the encoding used is unknown. My app has being using NSUnicodeStringEncoding (which is the same as NSUTF16StringEncoding) so have problems reading other than UTF16 encoded files. Is there a way I can guess the encoding of a file? My priority is to be able to read UTF8 files and then all other files. Is iterating through available encodings and check if read string's length is more than zero is really a good approach? Thanks in advance. Ignacio Ole Begemann Apple's documentation has some guidance on how to proceed: String

Case-insensitive NSString comparison

て烟熏妆下的殇ゞ 提交于 2019-12-03 12:45:40
问题 Using this code I am able to compare string values. [elementName isEqualToString: @"Response"] But this compares case-sensitively. Is there a way to compare the string without case sensitivity? 回答1: There’s a caseInsensitiveCompare: method on NSString , why don’t you read the documentation? The method returns NSComparisonResult : enum { NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending }; typedef NSInteger NSComparisonResult; …ah, sorry, just now I realized you are asking for case

How to check whether a string contains white spaces

雨燕双飞 提交于 2019-12-03 10:58:18
问题 How to check whether a string contains whitespaces in between characters? 回答1: use rangeOfCharactersFromSet: NSString *foo = @"HALLO WELT"; NSRange whiteSpaceRange = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]; if (whiteSpaceRange.location != NSNotFound) { NSLog(@"Found whitespace"); } note: this will also find whitespace at the beginning or end of the string. If you don't want this trim the string first... NSString *trimmedString = [foo

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

倾然丶 夕夏残阳落幕 提交于 2019-12-03 10:36:58
问题 I'm working on a new version of my app and am attempting to replace deprecated messages, but am not able to get past this one. I can't figure out why drawInRect:withAttributes is not working. The code displays properly when drawInRect:withFont:lineBreakMode:alignment message is sent, but does not work when drawInRect:withAttributes is sent. I'm using the same rect and font and I what I believe is the same text style. The constants are just positioning the rect just below an image, but I'm