"C # my sister and Aunt Objective-C" (03) NSString -- meet dog again

Source: Internet
Author: User
* ** Next to "C # my sister and Aunt Objective-C" (02), this is a Class-aunt's dog *** C # my sister: Wow, the road is narrow, isn't this an Objective-C aunt's dog? Dog: Who is it ..
C # sister: Not convinced ~ Let's get you another toast ..
# Import <Foundation/Foundation. h> # import "Dog. h "int main (int argc, const char * argv []) {ngutoreleasepool * pool = [[ngutoreleasepool alloc] init]; Dog * d = [Dog new]; [d Eat: @ "poop" EatQty: 1]; [d release]; [pool drain]; return 0 ;}

Dog:
C # sister: Fuck, upgrade it. Let's look at your skin and destroy your source code ..
Aunt Objective-C: why is your little girl always having trouble with my dog.
C # sister: It just doesn't eat shit. What else do you say "you keep yourself to eat ...", I am sure there is a problem with the program. I will help him debug it.
Aunt Objective-C: I was afraid that you would harm him again. I upgraded him yesterday... The code looks like this now ..

# Import "Dog. h" @ implementation Dog-(void) Sound {NSLog (@ "Wang! ");} BOOL isTrueFood (NSString * food) {nsange r = [@", steamed stuffed bun, bone, meat, ice cream, "rangeOfString: [NSString stringWithFormat: @ "% @", @ ",", food, @ ","]; if (r. length> 0) return (YES); else return (NO);}-(void) Eat :( NSString *) food EatQty :( int) count {if (isTrueFood (food) = YES) {NSLog (@ "Eat % d % @", count, food);} else {NSLog (@ "you can eat it yourself... ") ;}}@ End // Dog
C # sister: Wow, you have used the sensitive word filtering technology that is special to tianchao... It's a nightmare in the same world...
Objective-C aunt: Can you reduce your poverty ....
C # sister: It seems that other users can understand this sentence.
Nsange r = [@ ", steamed stuffed bun, bone, meat, ice cream," rangeOfString: [NSString stringWithFormat: @ "% @", @ ",", food, @ ","];
Objective-C Ayi: Two NSString methods are used in this statement. One is the instance method rangeOfString, and the other is the class method stringWithFormat. NSString is a class used for String processing in Cocoa, which is much more convenient than the original String array in C. Just like the. NET String, there are many built-in methods. RangeOfString: The function is similar to the String. IndexOf in. NET to determine the position of a String in another String, but the returned type is "nsange", instead of the definition of the number "nsange". You will know what is going on at first glance.
Typedef struct _ nsange {NSUInteger location; // start position NSUInteger length; // length} nsange;
In fact, nsange. location is equivalent to the return value of String. IndexOf in. NET.
C # sister:. in NET, if IndexOf cannot retrieve the corresponding content, a negative number is returned, and the location is of the NSUInteger type. It seems that this type is an unsigned integer. How does one indicate that no content can be retrieved? Objective-C Ayi: There are two ways to solve this problem. One is to judge the length. If the length is 0, the corresponding content is not found, and the other is to determine whether the location is equal to NSNotFound. In fact, NSNotFound is the maximum value of NSUInteger LONG_MAX.
C # sister: It's classic. In. NET, content cannot be retrieved using a negative number, and in Objective-C, the maximum value is used.
Objective-C aunt: Yes, rangeOfString usage is similar to this
NSString * word = @ "tive"; NSString * str = @ "this is an Objective-C Dog! "; NSLog (@" Search '% @' ", str, word in string '% @'); nsange r = [str rangeOfString: word]; // search for the word in str and return the result that contains {location and length} if (r. location = NSNotFound) // determine if (r. length <= 0) is the same as NSLog (@ "% @", word is not found in this sentence ); else NSLog (@ "in this sentence, the % lu character is % @", r. location + 1, r. length, word );
RangeOfString can also have a parameter to determine whether it is case sensitive or not. For example
Nsange r = [str rangeOfString: word options: NSCaseInsensitiveSearch]; // NSCaseInsensitiveSearch indicates case insensitive
If you still get dizzy, review the method in which Objective-C sends messages to the object. Another function is used. StringWithFormat:Similar to string. Format in. NET, it is also a class method and is used to generate a string. Usage: See the following example.

NSString * str2 = [NSString stringWithFormat: @ "% @ %@", @ "", @ ""]; NSLog (@ "% @", str2 ); // output this sentence.

To understand the two methods, we should understand the following statements.

Nsange r = [@ ", steamed stuffed bun, bone, meat, ice cream," rangeOfString: [NSString stringWithFormat: @ "% @", @ ",", food, @ ","];

In fact, the nesting of the two methods first sends the message stringWithFormat to the NSString class to generate the string of the food name, such as @ ", steamed stuffed bun," and then @ ", steamed stuffed bun, bone, meat, ice cream, "Actually, the NSString instance sends the message rangeOfString retrieval @", steamed stuffed bun, "whether it is included in the string @", steamed stuffed bun, bone, meat, ice cream. C # sister: A piece of tangled code ....
Aunt Objective-C: Now let's compare NSString with System. String in. NET. About the string length. NSString uses the length method. System. String has the Length attribute. Both of them fully consider the problem of Chinese characters. Whether it is English or Chinese, the length of each character is increased by 1. For string comparison.
Objective-C cannot use "=" to compare string content. "=" retains the original reference comparison function. C # has processed "=" as an intuitive comparison of content. The correct method for comparing strings in Objective-C is isEqualToString. Similar to. NET's System. String, NSString is also an immutable NSString. The so-called change is to generate a new String. Like the. NET String. Text. StringBuilder, Objective-C provides NSMutableString, which can be modified. But what is different from StringBuilder is that it inherits from NSString, which means that all methods of the base class can be used.

Dear colleagues, it takes a short time to learn Objective-C. learning Objective-C is not for Mac or iPhone development, but not practical,
Actually, it is a C # Learning note for the user to learn Objective-C. The exact purpose of learning is to help me understand C #After all, it is impossible to know the characteristics of C # without comparison.
Please take a critical look at this.If it is found that there are conflicts with other articles, books, comments, and materials, please refer to other articles as far as possible. And leave me a message
I also invite all experts to make bricks actively. I just used it to build a house ~~~
C # dialogue between sister and Aunt Objective-C

(01) meet Objective-C: greetings from the first meeting
(02) This is an aunt's dog.
(03) NSString -- meet another dog
(04) Basis of garbage collection-about the demolition team

Pending renewal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.