의 새 줄에 텍스트를 추가하려고 UILabel
합니다. 이제 현재 텍스트를 대체합니다.
UILabel
합니까?UILabel
합니까?@IBAction func sign(sender: AnyObject) {
if (ForUs.text == ""){
input1 = 0
} else{
input1 = Int((ForUs.text)!)!
}
if (ForThem.text == ""){
input2 = 0
} else {
input2 = Int((ForThem.text)!)!
}
ForUs.text?.removeAll()
ForThem.text?.removeAll()
input1total += input1
input2total += input2
Us.text = "\(input1total)"
Them.text = "\(input2total)"
if ( input1total >= 152){
print("you win")
}
if (input2total >= 152){
print("you lose")
}
}
게시 한 코드에 많은 문제가 있습니다.
우선 코드를 명확하게 만드십시오. 코드를 복사하여 예를 들어 플레이 그라운드에 붙여 넣으면 작동합니다. 때로는 이것이 가능하지 않지만 귀하의 경우에는 가능합니다.
코드 문제 :
String
에서 Int
직접 변환 할 수 없습니다.이 메서드는 a String
를 Int
선택적인 값을 생성하지 않는 방식으로 변환 합니다.
// elaborate for extra clarity
let forUsTextNSString = forUsText as NSString
let forUSTextFloat = forUsTextNSString.floatValue
input1 = Int(forUSTextFloat)
이것은 업데이트 된 코드이며 이제 컴파일됩니다.
// stuff I used to test this
var forUs = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
var forThem = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
var us = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
var them = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
// more stuff I used to test this
var input1 : Int = 0
var input2 : Int = 0
var input1total : Int = 0
var input2total : Int = 0
func sign() { // changed to non IB method, don't copy and paste this
// unwrap some optionals (google nil coalescing operator)
let forUsText = forUs.text ?? ""
let forThemText = forThem.text ?? ""
var usText = us.text ?? ""
var themText = them.text ?? ""
// elaborate way to convert String to Int (empty string returns a 0)
let forUsTextNSString = forUsText as NSString
let forUSTextFloat = forUsTextNSString.floatValue
input1 = Int(forUSTextFloat)
// compact method
input1 = Int((forUsText as NSString).floatValue)
input2 = Int((forThemText as NSString).floatValue)
forUs.text = ""
forThem.text = ""
input1total += input1
input2total += input2
us.text = "\(input1total)"
them.text = "\(input2total)"
if ( input1total >= 152){
print("you win")
}
if (input2total >= 152){
print("you lose")
}
}
이제 질문에 답하십시오.
UILabel
속성이 있습니다 numberOfLines
\n
텍스트에 줄 바꿈을 삽입하는 데 사용됩니다.numberOfLines
새 텍스트 \n
앞에 새 텍스트를 늘리고 추가합니다 .
usText += "\n\(input1total)"
themText += "\n\(input2total)"
// change += 1 to = 2 if that is what you actually need
us.numberOfLines += 1
them.numberOfLines += 1
us.text = usText
them.text = themText
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다