nsstring

Weak NSString variable is not nil after setting the only strong reference to nil

与世无争的帅哥 提交于 2019-12-17 04:02:11
问题 I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); I'm expecting all pointers to be nil at this time, but they are not and I don't understand why. The first (strong) pointer is nil but the other two are not. Why is that? 回答1: tl; dr:

Comparing Strings in Cocoa

人盡茶涼 提交于 2019-12-17 02:31:44
问题 I have tried: - (NSString*) generateString { NSString* stringToReturn = @"thisString"; return stringToReturn; } - (void) otherMethod { NSString *returnedString = [self generateString]; if (returnedString == @"thisString") { // Do this } else if (returnedString == @"thatString") { // Do that } } Which never matches. I have then tried if ([returnedString compare:@"thisString"] == 1) But the compare method always returns 1 for me, even when comparing with a different string. What is the correct

Number of occurrences of a substring in an NSString?

痞子三分冷 提交于 2019-12-17 02:09:09
问题 How can I get the number of times an NSString (for example, @"cake" ) appears in a larger NSString (for example, @"Cheesecake, apple cake, and cherry pie" )? I need to do this on a lot of strings, so whatever method I use would need to be relatively fast. Thanks! 回答1: This isn't tested, but should be a good start. NSUInteger count = 0, length = [str length]; NSRange range = NSMakeRange(0, length); while(range.location != NSNotFound) { range = [str rangeOfString: @"cake" options:0 range:range]

How do I convert an NSString value to NSData?

两盒软妹~` 提交于 2019-12-17 01:21:29
问题 How do I convert an NSString value to NSData ? 回答1: NSString* str = @"teststring"; NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; 回答2: NSString *str = @"helowrld"; // This converts the string to an NSData object NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; you can take reference from this link 回答3: Do: NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding]; then feel free to proceed with NSJSONSerialization:JSONObjectWithData . Correction to the

String replacement in Objective-C

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 00:32:01
问题 How to replace a character is a string in Objective-C? 回答1: You could use the method - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement ...to get a new string with a substring replaced (See NSString documentation for others) Example use NSString *str = @"This is a string"; str = [str stringByReplacingOccurrencesOfString:@"string" withString:@"duck"]; 回答2: NSString objects are immutable (they can't be changed), but there is a mutable

IOS 高级内存管理 ARC (Automatic Reference Counting) 原理分析

限于喜欢 提交于 2019-12-16 22:00:05
ARC rules 1.你不能够清晰的调用dealloc,implement或者invoke retain, release, autorelease,同时也不能够 使用衍生品,比如@selector(retain),@selector(release) 但是你可以implement dealloc,但是在写的这个dealloc中间不能够release instance variable,也不能够调用 [super dealloc],这些系统都帮你办了,但是你可能需要写[classInstance setDelegate:nil]这样的不能由ARC 覆盖的内容。你依然可以使用CFRetain, CFRelease 2.你不能使用NSAllocateObject和NSDeallocateObject,你可以使用alloc来创建物体,但是系统会自动dealloc 3.你不能在C struct中间使用object的指针,你可以使用objective c的class来做同样的事情 4.不能够在id和void*之间随意转换 5.不能使用NSAutoReleasePool Object,取代它的是@autoreleasepool block,后者效率更高 6.不能使用NSZone 7.命名的时候access不能以new打头,也就是说不能定义 @property NSString*

Converting HEX NSString To NSData

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-16 21:29:32
问题 I'm trying to convert a Hex NSString to NSData (I'm using the below attached code). The following is the output: <00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000> which looks totally irrelevant to me. Any idea/ suggestions on where its going wrong? NSString *strData = @"72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553"; NSLog(@"string Data length is %d",[strData length]); NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole

Remove all but numbers from NSString

為{幸葍}努か 提交于 2019-12-16 20:55:14
问题 I have an NSString (phone number) with some parenthesis and hyphens as some phone numbers are formatted. How would I remove all characters except numbers from the string? 回答1: Old question, but how about: NSString *newString = [[origString componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]; It explodes the source string on the set of non-digits, then reassembles them using an empty string separator. Not as efficient as

NSRange to Range<String.Index>

房东的猫 提交于 2019-12-16 19:54:53
问题 How can I convert NSRange to Range<String.Index> in Swift? I want to use the following UITextFieldDelegate method: func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { textField.text.stringByReplacingCharactersInRange(???, withString: string) 回答1: The NSString version (as opposed to Swift String) of replacingCharacters(in: NSRange, with: NSString) accepts an NSRange , so one simple solution is to convert String to

Convert Hex string to IEEE 754 float

二次信任 提交于 2019-12-16 18:03:44
问题 I am trying to convert a nsstring with hex values into a float value. NSString *hexString = @"3f9d70a4"; The float value should be = 1.230 . Some ways I have tried to solve this are: 1.NSScanner -(unsigned int)strfloatvalue:(NSString *)str { float outVal; NSString *newStr = [NSString stringWithFormat:@"0x%@",str]; NSScanner* scanner = [NSScanner scannerWithString:newStr]; NSLog(@"string %@",newStr); bool test = [scanner scanHexFloat:&outVal]; NSLog(@"scanner result %d = %a (or %f)",test