What is string?

what is string is it a data type or class ?

var somename:String="sdsa"

somename.append("sd")

we can able to conclude that

somename is a variable but how does the append generated

it should been generated only when this happens
var somename =String()(we have instance so that we can access its properties)

but how is append came when var somename:String
what is the role of colon symbol

what is string is it a data type or class ?

A String is a struct. You can read about structs and how they differ from classes here: https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html

it should been generated only when this happens
var somename =String()

A variable can be initialized with a literal. These are all identical:

var some1 = "Hello World"
var some2: String = "Hello World"
var some3 = String("Hello World")

what is the role of colon symbol

It's a type annotation, which you can read about here: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

1 Like

how to create custom struct like String
and make that custom struct behave as like string Struct?

These questions are very general, and I can't be more specific in my answer than referring you to a few chapters in the Swift Book.

Structs are covered starting at the section I linked above and continue on with the following few chapters.

https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html

I recommend reading over the entire Language Guide section in the Swift Book.

1 Like
Terms of Service

Privacy Policy

Cookie Policy