Objective-C (IOS) Basic Object operations (continuous improvement ..)

Source: Internet
Author: User

1. data type.
Int % O
Float % F
Couble %
Char % C

2. instance variable scope commands:
@ Protected the instance variable can be directly accessed by the class and any method defined in the subclass (default ).
@ Private instance variables can be directly accessed by methods defined in this class, and cannot be accessed directly by methods defined in the quilt class.
@ Public instance variables can be directly accessed by the methods defined in this class, or by methods defined in other classes or modules.
Q. So that other methods or functions can access instance variables through (->) (not recommended ).
@ Package for 64-bit images, you can access this instance variable anywhere where the image of this class is implemented.

 

3. String
Nsstring
Nsmutablestring (variable)
Nsmutablestring * string = [nsmutablestring stringwithcapacity: 42];
Appendstring: Or appendformat: Can Be Used to append a new string:
-(Void) appendstring :( nsstring *) astring;
-(Void) appendformat :( nsstring *) format ,...;
You can use deletecharactersinrange to delete characters in a string.
-(Void) deletecharactersinrange: (nsange) range;

Collection family:
Nsarray: used to store an ordered list of objects (any type of objects)
Restrictions: only objective-C objects can be stored. Basic data types (INT, float, Enum, struct, or nsarray) of the C language cannot be stored.
Random pointer ). At the same time, nil cannot be stored (the zero or null value of the object)
// Create a new nsarray
Nsarray * array = [nsarray arraywithobjects: @ "one", @ "two", nil];
// Obtain the number of objects contained
-(Unsigned) count;
// Obtain the object at the specified index
-(ID) objectatindex :( unsigned INT) index;

Split array:
Use componentsseparatedbystring: To split nsarray,
Nsstring * string = @ "Oop: ACK: Bork: greeble: ponies ";
Nsarray * chunks = [String componentsseparatedbystring: @ ":"];
Use componentsjoinedbystring: to merge elements in nsarray and create strings.
String = [chunks componentsjoinedbystring: @ ":-)"];
Nsarray is an unchangeable array, and the objects contained in the array can be changed, but the array object itself will not change.
Variable array nsmutablearray creates a variable array using the class method arraywithcapacity:
+ (ID) arraywithcapacity :( unsigned) numitems;
Nsmutablearray * array = [nsmutablearray arraywithcapacity: 17];
Use addobject: add an object to the end of the array
-(Void) addobject :( ID) anobject

Delete objects of a specific index
-(Void) removeobjectatindex :( unsigned) index;
Note: Variable Arrays can also insert objects in specific indexes, replace objects, and sort arrays. nsarray also provides a lot of useful functions.

Enumeration:
Nsenumerator is used to describe the operation method of this set iterator:
To use nsenumerator, You need to request the enumerator from the array through objectenumerator:
-(Nsenumerator *) objectenumerator;
You can use this method as follows:
Nsenumerator * enumerator;
Enumerator = [array objectenumerator];
Note: You can use the reverseobjectenumerator method to enumerate the set from the back to the front.
After obtaining the enumerator, start the while loop. Each loop requests its nextobject from this enumerator.
-(ID) nextobject; // The returned nil indicates that the loop ends.
Note: The array container cannot be changed during enumeration.
Quick enumeration example:
For (nsstring * string in array ){
Nslog (@ "I found % @", string );
}

 

Connection string

    1. Nsarray * strings = ...;
    2. String = [Strings componentsjoinedbystring: @ ""];
    3. // Result: string1 :-) string2 :-)......

 

Split string

    1. Nsstring * string = @ "string1: string2: string3: string4: string5 ";
    2. Nsarray * chunks = [String componentsseparatedbystring: @ ":"];

 

Convert to numeric value

    1. Nsstring * string =...; int intvalue = [String intvalue]; // int Value
    2. Float floatvalue = [String floatvalue]; // float Value
    3. Double doublevalue = [String doublevalue]; // double value

 

Remove line breaks from strings

    1. You can use the separator strings mentioned above and then connect them. However, the following method is better.
    2. Nsmutablestring * mstring = [nsmutablestring stringwithstring: String];
    3. Nsange wholeshebang = nsmakerange (0, [mstring length]);
    4. [Mstring replaceoccurrencesofstring: @ "" withstring: @ "" Options: 0 range: wholeshebang];

This method can also be used to replace strings. This method is a little faster than the method for separating/concatenating strings, but it is not obvious. Processing a string with a length of 1500000 contains 36909 line breaks, and the splitting/connection method takes 0.124 seconds. This method takes 0.071 seconds.

 

Obtain a substring

    1. Nsange range = [[string name] rangeofstring: otherstring options: nscaseinsensitivesearch];

 

Returns the string of the current date.

    1. Generally, nsdateformatter is used to convert a date to an nsstring, but sometimes it is easier to convert it using a special method. For example, you want to get: "December 4, 2007"
    2. [[Nsdate date] descriptionwithcalendarformat: @ "% B % E, % Y" timezone: Nil locale: Nil]

 

Trim an nsstring

    1. Nsstring * ook = @ "\ n \ t hello there \ t \ n ";
    2. Nsstring * trimmed = [ook stringbytrimmingcharactersinset:
    3. [Nscharacterset whitespaceandnewlinecharacterset];
    4. // The result is hello there.

 

Process a string by line

If each row is a piece of data in the style of this article, it is easy to process each piece of data cyclically. See the following example:

    1. Nsmutablearray * altitudes = [nsmutablearray array];
    2. Nsstring * altitudestring = [self altitudestringfromgoogle: coords];
    3. [Altitudestring enumeratelinesusingblock: ^ (nsstring * line, bool * Stop ){
    4. Float value = [Line floatvalue];
    5. [Altitudes addobject: [nsnumber numberwithfloat: value];
    6. }];

 

 

 

 

 

 

 

 

How to place a string on the clipboard

    1. @ implementation nsstring (pasteboardgoodies)
    2. -(void) sendtopasteboard
    3. {
    4. [[nspasteboard generalpasteboard]
    5. declaretypes: [nsarray arraywithobject: nsstringpboardtype]
    6. owner: Nil];
    7. [[nspasteboard generalpasteboard]
    8. setstring: Self fortype: nsstringpboardtype];
    9. }
    10. // sendtopasteboard @ end // pasteboardgoodies
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.