playgroundで仮に作ってみたものを実際にストーリーボードでつくってみるとうまく動きませんでした
textViewに複数列文字列をペーストして試してみたのですが、うまく配列を得るにはどうすればよいのか教えて下さい
これをプレイグラウンドでつくったときはうまくいきました
swift
1 2import Foundation 3 4var a:String = """ 5New 52-Week Highs: 134 6HIGH PRICE CHG CHG % VOLUME 7Western Asset Corporate Loan Fund Inc. 10.18 10.18 0.01 0.10 17.1K 8Western Asset Inflation-Linked Opportunities & Income Fund 11.48 11.48 0.04 0.35 114.7K 9WestRock Co. 43.36 42.76 -0.42 -0.97 970.4K 10WNS (Holdings) Ltd. ADR 66.13 65.85 0.27 0.41 58.3K 11Wyndham Destinations Inc. 51.97 51.49 -0.30 -0.58 198.6K 12Wyndham Hotels & Resorts Inc. 63.30 62.59 -0.40 -0.64 365.5K 13Zimmer Biomet Holdings Inc. 151.61 150.87 -0.37 -0.24 245.0K 14New 52-Week Lows: 5 15LOW PRICE CHG CHG % VOLUME 16Amira Nature Foods Ltd. 6.50 7.81 1.18 17.87 84.4K 17Aurora Cannabis Inc. 1.90 1.99 -0.02 -1.00 27.6M 18Ferrellgas Partners L.P. 0.31 0.32 -0.03 -8.86 578.7K 19RTW Retailwinds Inc. 0.72 0.76 0.02 2.40 256.7K 20SC Health Corp. Wt 1.04 1.04 -0.01 -0.95 1.1K 21""" 22 23var jjj:[String] = a.components(separatedBy: "New 52-Week Lows:") 24var kk = String(jjj[0]) 25print(kk) 26var b:[String] = kk.components(separatedBy: " ") 27 28 29let words = ["\n"] 30var sss = b.filter { b in 31 words.allSatisfy { b.contains($0) } 32} 33sss.removeFirst() 34 35var ords = ["Fund", "Series", "Pfd", "REIT", "Notes", "%","("] 36 37sss = sss.filter { b in 38 ords.allSatisfy { !b.contains($0) } 39} 40print(sss) 41 42var whileCounter = 0 43var Last = [String]() 44var sssCount = sss.count 45 46while whileCounter < sssCount { 47 if sss[whileCounter].contains("\n") { 48 let za = sss[whileCounter] 49 let de = za.components(separatedBy: "\n") 50 let www:[String] = [de[1]] 51 Last = Last + www 52 whileCounter += 1 53 } 54} 55print(Last) 56 57
以下をストーリーボードで試すと配列sssが空になってしまいます
textViewに入れた文字列はこれです
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
New 52-Week Highs: 132
HIGH PRICE CHG CHG % VOLUME
Aberdeen Global Premier Properties Fund 6.56 6.55 0.03 0.46 318.2K
AES Corp. 19.98 19.93 0.17 0.84 620.8K
Agilent Technologies Inc. 85.64 85.37 0.07 0.08 191.2K
Air Lease Corp. 48.58 48.24 -0.12 -0.25 94.9K
Alibaba Group Holding Ltd. ADR 216.01 215.91 1.65 0.77 3.6M
Allergan PLC 192.27 191.58 0.16 0.08 337.2K
New 52-Week Lows: 5
LOW PRICE CHG CHG % VOLUME
Capital Senior Living Corp. 3.10 3.17 -0.12 -3.65 98.3K
Ferrellgas Partners L.P. 0.30 0.31 -0.01 -4.08 615.2K
HEXO Corp. 1.55 1.55 -0.41 -20.66 9.2M
Orange ADR 14.52 14.56 -0.03 -0.20 198.8K
RYB Education Inc. ADR 5.46 5.46 -0.18 -3.17 11.2
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
swift
1 2import UIKit 3import Foundation 4 5class ViewController: UIViewController,UITextViewDelegate{ 6 7 @IBAction func doit(_ sender: UIButton) { 8 9 let a = String(textA.text) 10 11 let jjj:[String] = a.components(separatedBy: "New 52-Week Lows:") 12 let kk = String(jjj[0]) 13 let ppp:[String] = kk.components(separatedBy: " ") 14 let words = ["\n"] 15 var sss = ppp.filter { b in 16 words.allSatisfy { b.contains($0) } 17 } 18 print(sss) 19 20 sss.removeFirst() 21 22 let ords = ["Fund", "Series", "Pfd", "REIT", "Notes", "%","("] 23 24 sss = sss.filter { b in 25 ords.allSatisfy { !b.contains($0) } 26 } 27 print(sss) 28 let last = sss.map { $0.components(separatedBy: "\n")[1] } 29 30 31 } 32 @IBOutlet weak var textA: UITextView! 33 override func viewDidLoad() { 34 super.viewDidLoad() 35 textA.text = "ここに" 36 textA.delegate = self 37 38 self.view.addSubview(textA) 39 } 40 41 42} 43 44
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。