I'm a beginner.I make a simple omikuji app with swift.
import UIKit
classViewController:UIViewController {
@IBOutlet varuranau: UIButton!
@ IBAction func Uranau (sender: UIButton) {
}
@ IBAction funcuranau (sender: UIButton) {
varkekka=""
varkazu=arc4 random_uniform(5)
switch kazu {
case4:
kekka = "Okichi"
case3:
kekka="Nakayoshi"
case2:
kekka = "Kokichi"
case1:
kekka = "Good luck"
case0:
kekka = "Bad"
default:
kekka = "Error"
}
kekka.text=kekka
}
When I wrote , an error occurred in kekka.text=kekka.
Should I have used print()?
I don't know what's wrong.
Someone else has answered how to use print
to display it (on the debugging console), so let's talk about what you want to do on the screen.
{some object}.text={string}
If you want to display the results using the formula, {some object}
must look like this:
(A) having the property text
(B) An object that can display the contents of its properties
(C) The display of the object must be reflected on the screen
The kekka
that is written in the {object}
location in your code contains instances of type String
, which do not satisfy any of the above (A) through (C).If you want the results to appear on the screen, you must add an object that controls the display and run {an object}.text={string}
on that object.
(1) On InterfaceBuilder (storyboard editor of Xcode), add UILabel
for displaying results to this ViewController
.
(2) Add a new @IBOutlet
to the ViewController
.
@IBOutletvarkekkaLabel:UILabel!
(3) Connect this @IBOutlet
with the UILabel
of (1).
(4) Rewrite the line for displaying the results to operate the kekkaLabel
above.
kekkaLabel.text=kekka
If you are in a situation where you say "I don't know what's wrong" around here, I recommend you review the basics of programming again.
An error occurred at kekka.text=kekka.
Should I have used print()?
I don't know what's wrong.
varkekka=""
The variable "kekka" is a type String variable because it declares .
kekka="Okichi"
You don't need to replace it again because it's substituted with .
If you want to see the results,
print(kekka)
You can see it in the
varkekka=""
The data type for kekka is String (guess based type)
because the variable declaration has been made.
Since kekka is not an object, it is wrong to use kekka.text.
By the way, Yun, what did you expect to happen in the line below? I can't give you any advice because the question doesn't say what you want to do.
kekka.text=kekka
© 2024 OneMinuteCode. All rights reserved.