質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

487閲覧

swift_0..50までの乱数をラベルに表示させたい

DrqYuto

総合スコア432

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2019/01/30 11:13

編集2019/02/02 09:58
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ページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

  1. Storyboard上のUILabelをViewController.swiftに紐づける
  2. 1で定義した変数のtextプロパティにiValueを代入する

という流れです。

投稿2019/01/31 15:27

thatnaoki

総合スコア32

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DrqYuto

2019/02/01 09:17

``` import UIKit class ViewController: UIViewController { override func viewDidLoad() { weak var random: UILabel! 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/ } ``` Ctrlで紐づけたらエラーになってしまいました。 Thread 1: signal SIGABRT
guest

0

ベストアンサー

「UILabel swift storyboard」で調べれば使い方出てきますので、
また、「型 キャスト swift」で調べて、数字を文字列に変換してください。

投稿2019/01/30 11:38

編集2019/01/30 23:53
hameji001

総合スコア639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DrqYuto

2019/02/01 09:24

intをStringに変換すればいいですか?
hameji001

2019/02/01 09:27

他の人にも同じような事を回答した気がしますが、 ①storyboardのUILabelをIBOutletとしてコードに接続。 ②iValue(Int型)をString型に変換 ③storyboardのUILabelにその変換した値を入れ、表示させる。
hameji001

2019/02/01 09:45

上記でエラーになるんですか??? iValueのとこまででは大丈夫?なのにですか???
hameji001

2019/02/01 09:48 編集

ちなみに、 let iValue = Int.random(in: 0 ... 50) let sNum1:String = String(iValue) print(sNum1) 上記三行を自分のプロジェクトに移植したら、 普通に問題なく動きましたよ。 エラーは違うところが原因だと思います。 下記の回答のCtrlで紐づけたのが残ってるのではないですか???
DrqYuto

2019/02/01 09:48

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) ```
DrqYuto

2019/02/01 09:51

ラベルを消したらうまくいきました。 後は紐づけがよく分かりません。
DrqYuto

2019/02/02 09:23

``` 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 とエラーになってしまいました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問