I'm trying to print a private state variable #State private var bio: String. I get an error:
(lldb) expr bio
error: expression failed to parse:
error: Couldn't lookup symbols: MyClass.bio.getter : Swift.String
However, when I made this variable internal, then I have a successful printout.
What are some ways to print the private variable without changing the access control?
Related
I am trying to generate an automatic password code, however, I get an error with the below code "Cannot convert value of type '[Any]' to specified type 'String'
Output of debugger as follows:
expression failed to parse:
error: Loops + Function.playground:7:25: error: cannot convert value of type '[Any]' to specified type 'String'
var passString:String = []
^~
warning: Loops + Function.playground:9:5: warning: immutable value 'n' was never used; consider replacing with '_' or removing it
for n in 0...5 {
^
_
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var passString:String = []
for n in 0...5 {
passString = passString + alphabet.randomElement()!
}
print(passString)
I am using VBScript for the first time as it is the only language that a program can read.
Relevant section of code
Dim input
Function printer ()
input = InputBox("Printer", "Input Required")
printer = input
End Function
The software then calls the value of 'printer', and everything works fine when I input numbers, but if I try any text, it throws the below error message
Error: 13, Type mismatch: 'CLng'
in the line:
I created a custom Logger for logging values for different modes like debug, release but I want that whenever I use the default print() Xcode should throw an error explaining the user to use the custom logger.
I referred this but want to throw error there and then rather than adding a build-phase.
Any Ideas ?
You could shadow the built-in print in your module and mark it "unavailable":
// swift 3:
#available(*, unavailable, message: "use Logger instead!")
internal func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
Swift.print(items, separator: separator, terminator: terminator)
}
// swift 2:
#available(*, unavailable, message="use Logger instead!")
internal func print(items: Any..., separator: String = " ", terminator: String = "\n") {
Swift.print(items, separator: separator, terminator: terminator)
}
The result looks like:
(There is still another instance of print<Target>(..., to output: inout Target) which I don't bother to hide, I guess no one will accidentally use that function.)
As shown, you could still use the qualified name Swift.print to refer to the real function in case of emergency.
Note that this will only affect your own module. You can't force users outside of your module to not use print.
I'm new to Swift and is trying to learn the concept of optional binding. I have came up with the following code:
let possibleNumber = Int("123")
possibleNumber.dynamicType
if let actualNumber = Int(possibleNumber){
print("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
print("\(possibleNumber) could not be converted to an int")
}
Xcode playground output error message:
value of optional type "int?" not unwrapped, did you mean to use "!" or "?"
However, when I added the "!" to if let actualNumber = Int(possibleNumber!){
let possibleNumber = Int("123")
possibleNumber.dynamicType
if let actualNumber = Int(possibleNumber!){
print("\(possibleNumber) has an integer value of \(actualNumber)")
} else {
print("\(possibleNumber) could not be converted to an int")
}
Xcode display another error message:
initialiser for conditional binding must have Optional type, not int
Why is this happening?
The result of
let possibleNumber = Int("123")
is an optional Int - Int?
Then you're trying to create another Int with
Int(possibleNumber)
which does not work because the initializer expects a non-optional type.
The error message is related to the initializer rather than to the optional binding.
Try this to get the same error message.
let possibleNumber = Int("123")
let x = Int(possibleNumber)
In your second example when you initialize an Int with an implicit unwrapped Int! argument you get a non-optional Int and the compiler complains about the missing optional.
In the if let construct
if let actualNumber = Int(possibleNumber!){
print("\(possibleNumber) has an integer value of \(actualNumber)")
}
you don't need to use the Int initializer. You simply need to write
if let actualNumber = possibleNumber {
print("\(possibleNumber) has an integer value of \(actualNumber)")
}
Now Swift will try to unwrap possibleNumber. If the operation does succeed the unwrapped value is put inside actualNumber and the THEN block executed.
Say I have a struct like this one:
struct MyStruct: CustomStringConvertible {
let myInt: Int
let myString: String
var description: String {
return "my int is \(myInt),\nand my string is \"\(myString)\""
}
}
Printing a description from the code works fine.
let myStruct = MyStruct(myInt: 3, myString: "hello")
print(myStruct)
This results in
my int is 3,
and my string is "hello"
Problems arise when I want to print myStruct's description from the debugger. po myStruct results in
▿ my int is 3,
and my string is "hello"
- myInt : 3
- myString : "hello"
Explicitly printing out its description doesn't help either, as po myStruct.description results in
"my int is 3,\nand my string is \"hello\""
I thought it might have to do with CustomDebugStringConvertible, so I added this code:
extension MyStruct: CustomDebugStringConvertible {
var debugDescription: String {
return description
}
}
Unfortunately, this doesn't change any of the outcomes at all.
Is there a way to just have
my int is 3,
and my string is "hello"
printed from the command line while debugging?
(lldb) expression print(myStruct)
my int is 3,
and my string is "hello"
you can defined your own 'command'
(lldb) help command
The following subcommands are supported:
alias -- Allow users to define their own debugger command
abbreviations. This command takes 'raw' input (no need to
quote stuff).
delete -- Allow the user to delete user-defined regular expression,
python or multi-word commands.
history -- Dump the history of commands in this session.
regex -- Allow the user to create a regular expression command.
script -- A set of commands for managing or customizing script commands.
source -- Read in debugger commands from the file <filename> and execute
them.
unalias -- Allow the user to remove/delete a user-defined command
abbreviation.