import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let iValue = Int.random(in: 0 ... 50) print(iValue) }//参考 https://dev.classmethod.jp/smartphone/iphone/wwdc18-random-number-generator/ }

0..50までの乱数は下のコンソールに表示できました。Main.storybordのラベルに表示させたいです。
紐付けはうまく行ったと思いますがまだエラーになってしまいました。

参考https://i-app-tec.com/ios/label.html
import UIKit class ViewController: UIViewController { @IBOutlet var label: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let iValue = Int.random(in: 0 ... 50) let sNum1:String = String(iValue) print(sNum1) label.text = sNum1; }//参考 https://dev.classmethod.jp/smartphone/iphone/wwdc18-random-number-generator/ }
エラーはなくなりましたがクラッシュします。Thread 1: breakpoint 1.1
https://qiita.com/yuta-sanada0089/items/22e94168b6e72b27ac05
https://www.kenschool.jp/blog/?p=1340
ブレークポイントは右クリックで消せましたが黒い画面が出ます。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
回答2件
0
- Storyboard上のUILabelをViewController.swiftに紐づける
- 1で定義した変数のtextプロパティにiValueを代入する
という流れです。
投稿2019/01/31 15:27
総合スコア32
0
ベストアンサー
「UILabel swift storyboard」で調べれば使い方出てきますので、
また、「型 キャスト swift」で調べて、数字を文字列に変換してください。
投稿2019/01/30 11:38
編集2019/01/30 23:53総合スコア639
intをStringに変換すればいいですか?
他の人にも同じような事を回答した気がしますが、
①storyboardのUILabelをIBOutletとしてコードに接続。
②iValue(Int型)をString型に変換
③storyboardのUILabelにその変換した値を入れ、表示させる。
参考 https://i-app-tec.com/ios/int-string-double.html
```
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let iValue = Int.random(in: 0 ... 50)
let sNum1:String = String(iValue)
print(sNum1)
}//参考 https://dev.classmethod.jp/smartphone/iphone/wwdc18-random-number-generator/
}
```
これでもエラーになってしまいました。
上記でエラーになるんですか???
iValueのとこまででは大丈夫?なのにですか???
ちなみに、
let iValue = Int.random(in: 0 ... 50)
let sNum1:String = String(iValue)
print(sNum1)
上記三行を自分のプロジェクトに移植したら、
普通に問題なく動きましたよ。
エラーは違うところが原因だと思います。
下記の回答のCtrlで紐づけたのが残ってるのではないですか???
iValueの乱数までは良かったです。
シミュレーターがクラッシュしました
```
2019-02-01 18:46:32.969829+0900 random[10103:13268114] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
2019-02-01 18:46:33.060439+0900 random[10103:13268114] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<random.ViewController 0x7fe99bd175b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key random.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104c3f1bb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010321d735 objc_exception_throw + 48
2 CoreFoundation 0x0000000104c3ed29 -[NSException raise] + 9
3 Foundation 0x0000000102c48de4 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
4 UIKitCore 0x0000000106fa3292 -[UIViewController setValue:forKey:] + 87
5 UIKitCore 0x000000010723a573 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x0000000104c2acfd -[NSArray makeObjectsPerformSelector:] + 317
7 UIKitCore 0x00000001072372b9 -[UINib instantiateWithOwner:options:] + 1814
8 UIKitCore 0x0000000106faa452 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
9 UIKitCore 0x0000000106faaddc -[UIViewController loadView] + 177
10 UIKitCore 0x0000000106fab0ee -[UIViewController loadViewIfRequired] + 175
11 UIKitCore 0x0000000106fab940 -[UIViewController view] + 27
12 UIKitCore 0x0000000107602c53 -[UIWindow addRootViewControllerViewIfPossible] + 122
13 UIKitCore 0x000000010760336e -[UIWindow _setHidden:forced:] + 294
14 UIKitCore 0x00000001076165c0 -[UIWindow makeKeyAndVisible] + 42
15 UIKitCore 0x00000001075c3833 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4595
16 UIKitCore 0x00000001075c8c2f -[UIApplication _runWithMainScene:transitionContext:completion:] + 1623
17 UIKitCore 0x0000000106de74e9 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
18 UIKitCore 0x0000000106df029c +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
19 UIKitCore 0x0000000106de7126 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 233
20 UIKitCore 0x0000000106de7ae0 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1085
21 UIKitCore 0x0000000106de5cb5 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 795
22 UIKitCore 0x0000000106de595f -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 435
23 UIKitCore 0x0000000106deaa90 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 584
24 UIKitCore 0x0000000106deb80e _performActionsWithDelayForTransitionContext + 100
25 UIKitCore 0x0000000106dea7ef -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 221
26 UIKitCore 0x0000000106def93a -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
27 UIKitCore 0x00000001075c744e -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
28 UIKitCore 0x000000010716bd09 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 357
29 FrontBoardServices 0x000000010fe012da -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448
30 FrontBoardServices 0x000000010fe0c443 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 271
31 FrontBoardServices 0x000000010fe0bb3a __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53
32 libdispatch.dylib 0x0000000106062602 _dispatch_client_callout + 8
33 libdispatch.dylib 0x0000000106065b78 _dispatch_block_invoke_direct + 301
34 FrontBoardServices 0x000000010fe40ba8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
35 FrontBoardServices 0x000000010fe40860 -[FBSSerialQueue _performNext] + 457
36 FrontBoardServices 0x000000010fe40e40 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
37 CoreFoundation 0x0000000104ba4721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
38 CoreFoundation 0x0000000104ba3f93 __CFRunLoopDoSources0 + 243
39 CoreFoundation 0x0000000104b9e63f __CFRunLoopRun + 1263
40 CoreFoundation 0x0000000104b9de11 CFRunLoopRunSpecific + 625
41 GraphicsServices 0x000000010ce401dd GSEventRunModal + 62
42 UIKitCore 0x00000001075ca81d UIApplicationMain + 140
43 random 0x00000001028f6d47 main + 71
44 libdyld.dylib 0x00000001060d8575 start + 1
45 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
```
ラベルを消したらうまくいきました。
後は紐づけがよく分かりません。
下記のサイトの「アプリから文字列を設定する」をよく読んでその通りやってください。
https://techacademy.jp/magazine/2767
```
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
@property (weak, nonatomic) IBOutlet UILabel *lblDisplay;
let iValue = Int.random(in: 0 ... 50)
let sNum1:String = String(iValue)
print(sNum1)
_lblDisplay.text = @sNum1;
}//参考 https://dev.classmethod.jp/smartphone/iphone/wwdc18-random-number-generator/
}
```
こうですか?
なぜかCtrlで紐付けられず直接書きました。
Expected expression in assignment
とエラーになってしまいました。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。

2019/02/01 09:17