nsstring

NSString to CFStringRef and CFStringRef to NSString in ARC?

…衆ロ難τιáo~ 提交于 2019-11-26 21:25:19
I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC? Same for going the opposite direction, CFStringRef to NSString in ARC? What is the correct way to do this without creating memory leaks? Typically NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString; and CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString; Now, if you want to know why the __bridge keyword is there, you can refer to the Apple documentation . There you will find: __bridge transfers a pointer between Objective-C and Core Foundation with no

Convert NSArray to NSString in Objective-C

前提是你 提交于 2019-11-26 21:11:48
I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C . Dave DeLong NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""]; One approach would be to iterate over the array, calling the description message on each item: NSMutableString * result = [[NSMutableString alloc] init]; for (NSObject * obj in array) { [result appendString:[obj description]]; } NSLog(@"The concatenated string is %@", result); Another approach would be to do something based on each item's class: NSMutableString * result = [[NSMutableString

Get parts of a NSURL in objective-c

感情迁移 提交于 2019-11-26 21:09:55
I have an NSString with the value of http://digg.com/news/business/24hr How can I get everything before the 3rd level? http://digg.com/news/ This isn't exactly the third level, mind you. An URL is split like that way: the protocol or scheme (here, http ) the :// delimiter the username and the password (here there isn't any, but it could be username:password@hostname ) the host name (here, digg.com ) the port (that would be :80 after the domain name for instance) the path (here, /news/business/24hr ) the parameter string (anything that follows a semicolon) the query string (that would be if you

NSString - how to go from “ÁlgeBra” to “Algebra”

隐身守侯 提交于 2019-11-26 21:08:56
问题 Does anyone knows hoe to get a NSString like "ÁlgeBra" to "Algebra", without the accent, and capitalize only the first letter? Thanks, RL 回答1: NSString has a method called capitalizedString: Return Value A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values. NSString *str = @"AlgeBra"; NSString *other = [str capitalizedString]; NSLog (@"Old: %@, New: %@", str,

ios文件保存路径问题

北城以北 提交于 2019-11-26 21:03:52
NSArray *path1=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);//数据所在缓存的绝对路径 NSArray *path2=NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);//数据所存桌面的绝对路径 NSArray *path3=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSLocalDomainMask, YES);//数据所在缓存的相对路径 NSArray *path4=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//数据所在文件documents的绝对路径 NSArray *path5=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);//数据所在文件Library/Documentation的绝对路径 NSArray *path6

If “a == b” is false when comparing two NSString objects

Deadly 提交于 2019-11-26 21:03:27
I have a class with an accessible method that passes back an NSString when called. [MyClass getMyString] The string variable in that class is actually assigned in the didSelectRowAtIndexPath: part of a table like this: myString = cell.textLabel.text; When I retrieve the string by calling that method, I assign it to another string in the class that called it and compare it to a string I have defined NSString *mySecondString; mySecondString = @"my value"; if(mySecondString == myString){ i = 9; } I have stepped through the code and every time it evaluates the if statement, it skips right past the

How do I draw an NSString at an angle?

自古美人都是妖i 提交于 2019-11-26 20:46:24
问题 Is there a set of string attributes I can specify that will draw the text at an angle when I call: [label drawAtPoint:textStart withAttributes:attributes]; 回答1: Here's an example that uses a transform to rotate the drawing context. Essentially it's just like setting a color or shadow, just make sure to use -concat instead of -set . CGFloat rotateDeg = 4.0f; NSAffineTransform *rotate = [[NSAffineTransform alloc] init]; [rotate rotateByDegrees:rotateDeg]; [rotate concat]; // Lock focus if

Replace only the first instance of a substring in an NSString

时光毁灭记忆、已成空白 提交于 2019-11-26 20:19:45
问题 So if you have an NSString that goes: @"My blue car is bigger than my blue shoes or my blue bicycle"; I would like a method that replaces only the first instance of blue with green, to produce: @"My green car is bigger than my blue shoes or my blue bicycle"; How does one do this? 回答1: Assuming the following inputs: NSString *myString = @"My blue car is bigger then my blue shoes or my blue bicycle"; NSString *original = @"blue"; NSString *replacement = @"green"; The algorithm is quite simple:

How to split long NSString into pages

人走茶凉 提交于 2019-11-26 20:15:31
问题 I have a long NSString I want to display over a couple of pages. But to do this, I need to find out how much text will actually fit on the page. [NSString sizeWithFont: ...] Is not enough, it will just tell me if the text fits in the rectangle or not, if its does not, it will silently truncate the string, but it won't tell me where it truncated! I need to know the first word that does not fit on the page, so I can split the string and draw that part of it on the next page. (and repeat) Any

How to make subscripts and superscripts using NSAttributedString?

送分小仙女□ 提交于 2019-11-26 20:09:15
I need to make subscripts for chemistry formulas (H2O, Na^2+, etc)? Is this possible to do with NSAttributedString, or is there an alternative/easier way to make subscripts? This is possible to do with NSAttributedString . The attribute constant you're looking for depends on your platform. For Mac OS X it is NSSuperscriptAttributeName and on iOS it is kCTSuperscriptAttributeName . Pass in a negative value for subscript. The only caveat is that UILabel on iOS can't draw NSAttributedString s (yet, fingers crossed for iOS 6). You would need to draw the text using Core Text or find some third