Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

The following code snippet results in a compile time error:

struct IntStack {

var items = [Int]()

func add(x: Int) {

items.append(x) // Compile time error here.

}

}

Explain why a compile time error occurs. How can you fix it?

Answer Posted / iosraj

Structures are value types. By default, the properties of a value type cannot be modified from within its instance methods.

However, you can optionally allow such modification to occur by declaring the instance methods as ‘mutating’; e.g.:

struct IntStack {

var items = [Int]()

mutating func add(x: Int) {

items.append(x) // All good!

}

}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between swift and ‘objective-c’ language?

792


Explain completion handler?

761


What is nil-coalescing operator?

745


In swift what is use of backticks while declaring a variable?

815


Is swift similar to c?

672


What is forced unwrapping? Why is it potentially unsafe?

747


What are the advantages of swift?

764


List the features of swift programming?

766


What do you do when you realize that your app is prone to crashing?

799


What are lazy stored properties, and how are they useful?

740


What is operation queue in swift?

782


Do loops swift?

720


What are structures in swift?

753


Which banks use swift?

705


What is optional binding?

815