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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

2回答

2556閲覧

Ambiguous use of 'subscript(_:)' というエラーが出ている原因がわかりません

ueda_kesuke

総合スコア34

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2019/06/14 13:12

編集2019/06/14 23:43

タグのミスなのか、バージョンなのかわからないエラー文が出てしまいました。

swift

1import UIKit 2 3class RecordViewController: UIViewController ,UITableViewDataSource, 4UITableViewDelegate{ 5 6 var aD:AppDelegate! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 // Do any additional setup after loading the view. 12 aD = UIApplication.shared.delegate as? AppDelegate 13 } 14 15 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> 16 Int { 17 return aD.records.count 18 } 19 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 20 var cell = tableView.dequeueReusableCell( withIdentifier: "Cell") 21 22 var nameLabel = cell?.viewWithTag(1) as! UILabel 23 nameLabel.text = aD.records[indexPath.row][0] as? String 24 25 var genderLabel = cell?.viewWithTag(2) as! UILabel 26 genderLabel.text = aD.records[indexPath.row][1] as? String 27 28 var scoreLabel = cell?.viewWithTag(3) as! UILabel 29 let score:Int = aD.records[indexPath.row][2] as! Int 30 scoreLabel.text = "(score)%" 31 32 return cell ?? 33 } 34 35 36

上記の
nameLabel.text = aD.records[indexPath.row][0] as? String
let score:Int = aD.records[indexPath.row][2] as! Int
genderLabel.text = aD.records[indexPath.row][1] as? String

の部分に「Ambiguous use of 'subscript(_:)'」というエラー文が出てしまいます。

また、最終行に「Expected expression after operator」というエラー文が発生してしまいました。

試したこと

main.boardのtagに該当の数字を入れているかチェックした。
nilなのかわからない部分を!と?どちらも試してみた。

他の方の参考サイトもみたのですが、わからなかったので質問させていただきました。

雑な質問で申し訳ないのですが、ご教授いただきたいです????‍♂️
足りない情報などありましたら、おっしゃっていただけるとありがたいです!

swift

1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 var name: String = "" 8 var gender: String = "" 9 var score: Int = 0 10 var records: [AnyObject] = [] 11 12 13 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 14 // Override point for customization after application launch. 15 return true 16 } 17 18 func applicationWillResignActive(_ application: UIApplication) { 19 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 20 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 21 } 22 23 func applicationDidEnterBackground(_ application: UIApplication) { 24 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 25 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 26 } 27 28 func applicationWillEnterForeground(_ application: UIApplication) { 29 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 30 } 31 32 func applicationDidBecomeActive(_ application: UIApplication) { 33 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 34 } 35 36 func applicationWillTerminate(_ application: UIApplication) { 37 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 38 } 39 40 41} 42 43

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

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

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

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

MasakiHori

2019/06/14 23:39

AppDelegateのソースソードを追記してください
ueda_kesuke

2019/06/14 23:43

ありがとうございます!追記いたしました。
guest

回答2

0

Swiftでは変数名の後に[]をつけて要素を取り出す操作をサブスクリプト subscript といいます。

swift

1aD.records[indexPath.row][0] as? String 2``` この操作は以下のように分解できます。 3 4```swift 5let aRecord = aD.records[indexPath.row] 6nameLabel.text = aRecord[0] as? String

まず、aD.recordsから要素を取り出し、その要素に対しsubscript の操作をしています。
さてここで、aRecordの型は何でしょう

swift

1var records: [AnyObject] = []

と定義されていますので、aRecordの型はAnyObjectです。
AnyObjectにはsubscriptはありませんのでエラーが発生します。

recordsの内容が分かりませんので、私には適切な修正方法はわかりませんが、recordsを適切な型の配列とすればよいのではないでしょうか。

投稿2019/06/15 01:00

MasakiHori

総合スコア3384

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

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

ueda_kesuke

2019/06/15 06:15

用語から解説いただきありがとうございます!!とてもわかりやすく理解できました。
guest

0

ベストアンサー

swift

1nameLabel.text = aD.records[indexPath.row][0] as? String

上記のようにアクセスするのでしたら、配列が2重になっている必要があります。
なので????の様にプロパティの型を変更するとエラーなくアクセスできます。

swift

1var records: [[AnyObject]] = []

最後の行のエラーはnil だった場合は初期化したのもを返せば問題ないので以下の様になると思います。

swift

1 2return cell ?? UITableViewCell()

投稿2019/06/15 00:43

_Kentarou

総合スコア8490

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

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

ueda_kesuke

2019/06/15 00:53

解決できました!!配列を二重にするという考えは全く思いつきませんでした...。ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問