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 are the half open range operators in swift?

852


What is forced unwrapping? Why is it potentially unsafe?

837


What are adapter and memento patterns?

910


Does swift have a garbage collector?

994


Is it worth learning swift 2019?

824


What is mutable and immutable in swift?

947


What is a delegate in swift?

885


How many types of closures are there in swift?

819


What is an optional in swift?

837


How to make a method or variable generics in swift?

831


What are the control transfer statements in swift?

906


What is mvvm in swift?

842


Can you explain completion handler?

855


Why do we use swift? Mention some advantages of swift?

901


How to convert nsmutablearray to swift array in swift?

887