nsstring

Convert NSString to NSInteger?

Deadly 提交于 2019-12-17 07:14:03
问题 I want to convert string data to NSInteger . 回答1: If the string is a human readable representation of a number, you can do this: NSInteger myInt = [myString intValue]; 回答2: [myString intValue] returns a cType "int" [myString integerValue] returns a NSInteger . In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ). 回答3: I've found this to be the

NSXMLParser详解

做~自己de王妃 提交于 2019-12-17 07:02:54
NSXMLParser 实现的是sax方法解析xml文件。 dom实现的原理是把整个xml文档一次性读出,放在一个树型结构里 。在需要的时候,查找特定节点,然后对节点进行读或写。他的主要优势是实现简单,读写平衡;缺点是比较占内存,因为他要把整个xml文档都读入内存,文件越大,这种缺点就越明显。 sax的实现方法和dom不同。他只在xml文档中查找特定条件的内容,并且只提取需要的内容 。这样做占用内存小,灵活,正好满足我们的需求。他的缺点就是写,有些资料介绍了写入的方法,但是我感觉这对本例没有必要。 运行NSXMLParser涉及设置、运行和响应结果。 1)启动NSXMLParser 要使用NSXMLParser要先创建它,设置各种属性,主要用到以下几个方法: initWithContentsOfURL 通过NSURL创建解析器 initWithData 通过NSData创建解析器 setDelegate 为解析器定义委托 parse 运行解析器 2)充当委托 最重要的5个方法: //发现元素开始符的处理函数 (即报告元素的开始以及元素的属性) - (void)parser:(NSXMLParser *)parser         didStartElement:(NSString *)elementName         namespaceURI:(NSString *

How to create subscript characters that's not in Unicode in iOS

安稳与你 提交于 2019-12-17 06:45:11
问题 I've been trying for a while now to create a NSString with subscripted character without success. Is it even possible to do this in iOS? I need a way to change characters in a string to subscript or superscript, and I can't use the Unicode for this as Unicode doesn't have all the letters. My guess could be to use the HTML tags <sub> and <sup> but I haven't find a way to convert said HTML tags to a NSString. 回答1: Subscript and superscript are not character traits. With few exceptions (e. g. ²,

iphone sdk - Remove all characters except for numbers 0-9 from a string [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-17 06:35:41
问题 This question already has answers here : Remove all but numbers from NSString (22 answers) Closed 5 years ago . OK So here's the plan. The XML I'm getting data from allows non-numeric text in phone number fields (for descriptions or contact names, etc). I am trying to extract only the numbers and call the tel: URL with them to initiate a call. Here's whats NOT working: NSCharacterSet *charset = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; NSString *number = @"(555) 555-5555 Office

How to convert a unichar value to an NSString in Objective-C?

≡放荡痞女 提交于 2019-12-17 06:29:34
问题 I've got an international character stored in a unichar variable. This character does not come from a file or url. The variable itself only stores an unsigned short(0xce91) which is in UTF-8 format and translates to the greek capital letter 'A'. I'm trying to put that character into an NSString variable but i fail miserably. I've tried 2 different ways both of which unsuccessful: unichar greekAlpha = 0xce91; //could have written greekAlpha = 'Α' instead. NSString *theString = [NSString

Convert NSArray to NSString in Objective-C

随声附和 提交于 2019-12-17 05:22:26
问题 I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C . 回答1: NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""]; 回答2: 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

Convert NSArray to NSString in Objective-C

别说谁变了你拦得住时间么 提交于 2019-12-17 05:22:05
问题 I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C . 回答1: NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""]; 回答2: 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

Split an NSString to access one particular piece

╄→гoц情女王★ 提交于 2019-12-17 04:46:16
问题 I have a string like this: @"10/04/2011" and I want to save only the "10" in another string. How can I do that? 回答1: NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"]; NSString* firstBit = [foo objectAtIndex: 0]; Update 7/3/2018: Now that the question has acquired a Swift tag, I should add the Swift way of doing this. It's pretty much as simple: let substrings = "10/04/2011".split(separator: "/") let firstBit = substrings[0] Although note that it gives you an array of Substring

Difference between NSString literals

*爱你&永不变心* 提交于 2019-12-17 04:32:43
问题 What is the difference between these two lines? NSString * string = @"My String"; NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease] 回答1: @"My String" is a literal string compiled into the binary. When loaded, it has a place in memory. The first line declares a variable that points to that point in memory. From the string programming guide: The simplest way to create a string object in source code is to use the Objective-C @"..." construct: NSString *temp = @"/tmp

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

↘锁芯ラ 提交于 2019-12-17 04:02:22
问题 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: