Swift3.0 language tutorial using pointers to create and initialize strings

Source: Internet
Author: User

Swift3.0 language tutorial using pointers to create and initialize strings

Swift3.0 Language Tutorials creating and initializing strings with pointers Apple's swift team has done a lot to support some of C's basic features. The C language provides pointers to us, and Swift is no exception, and developers can use pointers to create and initialize strings.

(1) in NSString the init (bytes:length:encoding:) method can get the specified number of characters from the given string by specifying the direction, and then use these characters to create and initialize the string. Its grammatical form is as follows:

Convenience init? (bytes:unsafepointer<void>, length len:int, encoding:uint)

Where bytes is used to specify a constant pointer, length is used to specify the number of bytes from the bytes used, and encoding is used to specify the character encoding to be applied in the byte.

"Example 1-9" The following will use the init (bytes:length:encoding:) method to create and initialize strings.

Import Foundation

var str=nsstring (bytes: "Hello,world", Length:8,encoding:string.encoding.utf8.rawvalue)

Print (str!)

The results of the operation are as follows:

Hello,wo

(2) The init (BytesNoCopy:length:encoding:freeWhenDone:) method also obtains the specified number of characters from the given string in the given direction, creating and initializing the string by these characters, However, this method needs to set the release of the data in memory. Its grammatical form is as follows:

Convenience init? (bytesnocopy bytes:unsafemutablepointer<void>, length len:int, Encoding:uint, Freewhendone Freebuffer:bool)

Among them, the parameters are described as follows:

Q bytes: Used to specify a variable pointer.

Q len: Used to specify the number of bytes from the bytes used.

Q Encoding: Used to specify the character encoding to be applied in the byte.

Q flag: Used to set the release of data in memory. When set to true, data that is no longer used in memory can be disposed of, otherwise, not released.

"Example 1-10" The following will use the init (BytesNoCopy:length:encoding:freeWhenDone:) method to create and initialize strings.

Import Foundation

var a=nsstring (string: "Hello,world")

var b=a.utf8string!

var c:unsafemutablepointer<void>=unsafemutablepointer<void> (b)

var str=nsstring (bytesnocopy:c,length:8,encoding:string.encoding.utf8.rawvalue,freewhendone:true)

Print (str!)

The results of the operation are as follows:

Hello,wo

(3) The init (characters:length:) method can obtain a specified number of elements from a C-language string, creating and initializing the string with the following syntax:

Convenience init (CHARACTERS:UNSAFEPOINTER<UNICHAR>, Length:int)

Where characters is used to specify a constant pointer, length is used to specify the number of characters from the character used.

"Example 1-11" The following will use the init (characters:length:) method to create and initialize strings.

Import Foundation

Let str1=nsstring (string: "Hello")

Let Char:[unichar]=[str1.character (at:0), Str1.character (at:1), Str1.character (At:2), Str1.character (At:3), Str1.character (At:4)]

var str2=nsstring (Characters:char,length:3)

Print (STR2)

The results of the operation are as follows:

Hel

(4) The init (CharactersNoCopy:length:freeWhenDone:) method also obtains a specified number of elements from a C language string, which creates and initializes the string, but this method needs to set the release of the data in memory. Its grammatical form is as follows:

Convenience init (charactersnocopy characters:unsafemutablepointer<unichar>, Length:int, FreeWhenDone Freebuffer:bool)

Among them, the parameters are described as follows:

Q characters: Used to specify a variable pointer.

Q Length: Used to specify the number of characters from the character used.

Q FreeBuffer: Used to set the release of data in memory. When set to true, data that is no longer used in memory can be disposed of, otherwise, not released.

"Example 1-12" The following will use the init (CharactersNoCopy:length:freeWhenDone:) method to create and initialize strings.

Import Foundation

Let a=nsstring (string: "Swift")

var b:[unichar]=[a.character (at:0), A.character (at:1), A.character (At:2), A.character (At:3), A.character (At:4)]

var str=nsstring (charactersnocopy: &b,length:3,freewhendone:true)

Print (str)

The results of the operation are as follows:

Swi

(5) The init (utf8string:) method can create and initialize a string in the form of a string in the UTF8 format, with the following syntax:

Convenience init? (Utf8string nullterminatedcstring:unsafepointer<int8>)

Where nullterminatedcstring is used to specify a constant pointer.

"Example 1-13" The following will use the init (utf8string:) method to create and initialize strings.

Import Foundation

var str=nsstring (utf8string: "Swift")

Print (str!)

The results of the operation are as follows:

Swift

(6) The init (cstring:encoding:) method can derive elements from a C language string, which creates and initializes the string in the following syntax form:

Convenience init? (cString nullterminatedcstring:unsafepointer<int8>, Encoding:uint)

Where nullterminatedcstring is used to specify a constant pointer, encoding is the nullterminatedcstring encoding.

"Example 1-14" The following will use the init (cstring:encoding:) method to create and initialize strings.

Import Foundation

var str=nsstring (cString: "Hello", Encoding:String.Encoding.utf8.rawValue)

Print (str!)

The results of the operation are as follows:

Hello

Swift3.0 language tutorial using pointers to create and initialize strings

Swift3.0 language tutorial using pointers to create and initialize strings

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.