我想在我的应用中找到current used language
,如以下链接:
How to get current language code with Swift?
let pre = Locale.preferredLanguages[0]
但是它一直在崩溃并且出现了这个错误:
如何解决这个问题以及为什么会出现此错误?
这只发生在以下类:
class ButtonText: UIButton {
let padding = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 0);
let padding2 = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 35);
let pre = Locale.preferredLanguages[0]
override open func titleRect(forContentRect bounds: CGRect) -> CGRect {
//if pre != "ar"{
if let code = Locale.current.languageCode , code != "ar" {
return UIEdgeInsetsInsetRect(bounds, padding)
}else {
titleLabel?.textAlignment = .left
return UIEdgeInsetsInsetRect(bounds, padding2)
}
/* }else{
titleLabel?.textAlignment = .left
return UIEdgeInsetsInsetRect(bounds, padding2)
}*/
}
}
答案 0 :(得分:1)
您的图片代码没有错误我们需要以前的代码以及您如何使用此按钮来了解异常的位置
此行的当前languageCode是可选字符串
<div class="contentContainer">
<div>Title</div>
<div>Posted by u/user1</div>
<div class="text">
Text
</div>
</div>
.contentContainer {
display: flex;
flex-direction: column;
margin-bottom: 15;
}
.text {
border: 1px solid black;
}
但这条线不是可选的
Locale.current.languageCode
所以试试这个并告诉我结果
Locale.preferredLanguages[0]