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

Source: Internet
Author: User

C # sister: Wow, the path 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 ..
View sourceprint? 01 # import <Foundation/Foundation. h>

02 # import "Dog. h"

03

04 int main (int argc, const char * argv [])

05 {

06

07 NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];

08 Dog * d = [Dog new];

09 [d Eat: @ "poop" EatQty: 1];

10 [d release];

11 [pool drain];

12 return 0;

13}

Dog:
C # sister: Fuck, upgrade it. Let's look at your skin and destroy your source code ..
Aunt Objective-C: Hey, 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: Sorry is afraid that you will harm it again. I upgraded it to him yesterday... The code looks like this now ..
View sourceprint? 01 # import "Dog. h"

02 @ implementation Dog

03-(void) Sound

04 {

05 NSLog (@ "Wang! ");

06}

07 BOOL isTrueFood (NSString * food)

08 {

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

10 if (r. length> 0)

11 return (YES );

12 else

13 return (NO );

14

15}

16-(void) Eat :( NSString *) food EatQty :( int) count

17 {

18 if (isTrueFood (food) = YES)

19 {

20 NSLog (@ "Eat % d kg % @", count, food );

21}

22 else

23 {

24 NSLog (@ "keep it for yourself... ");

25}

26}

27 @ 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: Sorry, can you reduce your poverty ....
C # sister: It seems that other users can understand this sentence.
View sourceprint? Nsange r = [@ ", steamed stuffed bun, bone, meat, ice cream," rangeOfString: [NSString stringWithFormat: @ "% @", @ ",", food, @ ","];

Objective-C aunt: 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: similar to the String. IndexOf in. NET, rangeOfString is used to determine the position of a String in another String. However, the returned type is nsange, not a number.
It is estimated that you will know what is going on at first glance in the definition of nsange.
View sourceprint? 1 typedef struct _ nsange {

2 NSUInteger location; // start position

3 NSUInteger length; // length

4} 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
View sourceprint? 1 NSString * word = @ "tive ";

2 NSString * str = @ "this is an Objective-C Dog! ";

3 NSLog (@ "Search '% @'", str, word in the string '% );

4 nsange r = [str rangeOfString: word]; // search for word in str and return the result that contains {location and length}

5 if (r. location = NSNotFound) // The same is true for if (r. length <= 0 ).

6 NSLog (@ "% @", word not found in this sentence );

7 else

8 NSLog (@ "in this sentence, the % lu character after 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
View sourceprint? 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 used
StringWithFormat: similar to string. Format in. NET, it is also a class method and is used to generate strings. Usage: See the following example.

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


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

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


Actually, it is the nesting of two methods.
First, send the stringWithFormat message to the NSString class to generate the string of the food name, such as @ ", steamed stuffed bun ,"
Then send the message rangeOfString retrieval @ ", steamed stuffed bun, bone, meat, ice cream," to the string @ ", which is actually an NSString instance @", steamed Stuffed Bun, bone, meat, ice cream, "medium.
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.
Same as System. String Of. NET, NSString is also immutable
The so-called NSString 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 the so-called C # without comparison.
Please take a critical look at this. If you find 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
We also invite all experts to make a good shot.

 

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.