What Is Lldb_Expr in Swift Playground

What is lldb_expr in Swift Playground?

I am not 100% about this but to me this seems sane and logical.

Your Animal class is empty so the compiler needs a way to express / print the class / it's values. So what it does is print __lldb_expr_380.Animal because the compiler doesn't know what else to make of it. If you add a property, for example legs, the result becomes this: {{legs 2} feet 4}.

So, in my understanding whenever you have this empty superclass the compiler will get 'confused' and the error that happens is that it will just print out __llb_expr_:some_number:.ClassName instead of something like {}.

Reference: http://discuss.codewithchris.com/t/episode-7-classes-error---lldb-expr-/150

What is this [__lldb_expr_19.Book] I'm getting in the console?

That's the way Swift describes classes.

For a more meaningful way add this extension to adopt CustomStringConvertible. In the computed property return whatever you want

extension Book : CustomStringConvertible {

var description : String { return "Book \(name) / \(author)" }
}

Do you really need a class? With a struct you get rid of the initializer, adopting Equatable doesn't require any additional code and you get a better description for free.

This can replace your entire class

struct Book : Equatable {
let title, author, genre: String
}

Empty Class in Swift Playground Gives __lldb_expr_ Error

This is not an error.

It's an information that the variable foo now holds an object of class FooBar whose internal name is __lldb_expr_12.FooBar. __lldb_expr_12 is the Swift module's name in the Playground in this case.

Error lldb_expr when printing a variable

It´s not an error, it´s the Low Level Debugger and how the Playground prints out your enum. If you break it down you´ll see:

[

__lldb_expr_98.Movie.MovieGenere.comedy,

__lldb_expr_98.Movie.MovieGenere.adventure,

__lldb_expr_98.Movie.MovieGenere.animation
]

Create random number of class instances - Swift

There are probably a lot of different (valid) approaches to this. One of which could be something like this:

let exerciseArray = ["squats", "pushups", "lunges", "jumping jacks"]

struct Exercise {
let name: String
let reps: Int
}

func randomInt(upperBound: UInt32) -> Int {
return Int(arc4random_uniform(upperBound))
}

let numberOfExercisesInWorkout = randomInt(upperBound: 10)

let exercises: [Exercise] = (1...numberOfExercisesInWorkout).map { _ in
let randomKey = randomInt(upperBound: UInt32(exerciseArray.count))
return Exercise(name: exerciseArray[randomKey], reps: randomInt(upperBound: 10))
}

print(exercises)

Which ouputs something like this:

[Exercise(name: "lunges", reps: 8), Exercise(name: "jumping
jacks", reps: 6), Exercise(name: "lunges", reps: 4),
Exercise(name: "squats", reps: 9), Exercise(name: "jumping
jacks", reps: 5), Exercise(name: "squats", reps: 9)]


Note: You could also modify the (randomInt) helper function to take into account a lowerBound I guess



Related Topics



Leave a reply



Submit