How to Print Array in Label?
I assumed your lapTime
is like below,
let lapTime: [[Double]] = [[33.34,22.876,34.0]]
Method 1:
let arr = lapTime[0].map { (double) -> String in
return String(double)
}.joined()
self.label.text = arr
or
let arr = lapTime[0].map { (double) -> String in
return String(double)
}.joined(separator: ", ")
self.label.text = arr
Method 2:
for time in lapTime[0]{
let arr = (self.label.text ?? "")+" "+String(time) //+"<space>"+ to +","+ if you need lap times are comma separated.
self.label.text = arr
}
Note:
The lapTime == [[33.34,22.876,34.0],[33.34,22.876,34.0]]
and you had a UITableView
, then replace lapTime[0]
to lapTime[indexPath.row]
.
let me know is that you need.
Output:
How to display array in label?
Conform MyCurrency
to CustomStringConvertible
and override its description
property to return
the required String
instance,
struct MyCurrency: CustomStringConvertible {
var currency: String
var currentPrice: String
var description: String {
return "\(currency) to \(currentPrice)"
}
}
Now, to get a String
of currencies, use map(_:)
and joined()
on the array
of MyCurrency
, i.e.
let btc = MyCurrency(currency: "BTC", currentPrice: "10223.4337")
let gbp = MyCurrency(currency: "GBP", currentPrice: "1,2221")
let rub = MyCurrency(currency: "RUB", currentPrice: "0.015169")
let eur = MyCurrency(currency: "EUR", currentPrice: "1.11")
let uah = MyCurrency(currency: "UAH", currentPrice: "0.039841")
let str = [btc, gbp, rub, eur, uah].map({ String(describing: $0) }).joined()
print(str) //BTC to 10223.4337GBP to 1,2221RUB to 0.015169EUR to 1.11UAH to 0.039841
label.text = str
Update:
You can use reduce(_:_:)
as well to get the required output, i.e.
let str = [btc, gbp, rub, eur, uah].reduce("") { (result, currency) -> String in
return result + String(describing: currency)
}
print(str) //BTC to 10223.4337GBP to 1,2221RUB to 0.015169EUR to 1.11UAH to 0.039841
How to display an array in a label
You can use Swift's join
method, as described in this question.
So in your case, it'd be:
self.aar.join("")
which would be the aar array joined on the empty string, meaning all elements concatenated (as long as they are strings themselves, which it seems they are).
How can i show an array in label ? With ,
An array of strings can be joined by ,
using:
arr.joined(separator: ",")
Refer: https://developer.apple.com/documentation/swift/array/joined(separator:)-5do1g
How can I write the items to UILabel from the array?
you can use joined()
function:
myLabel.text = "\(array.joined())\n"
How to make an array the label text?
First, and easiest, idea that comes to mind, is just make 3 labels and constrain them as you wish. Secondly, you could create a UITextView, instead.
let tView = UITextView(frame: CGRect(x: 0, y: 0, width: widthOfLongestWord, height: whatever height fits 3 lines))
tView.text = ""
for str in array {
tView.text += str + "\n"
}
I assume this is being used in a view controller, in which case you should set the frame to zero and just constrain the textView with autoLayout.
Display two array data in label swift
Simply use zip(_:_:)
, map(_:)
and joined(separator:)
on the arr1
and arr2
to get the expected result, i.e.
let arr1 = ["A","B","C"]
let arr2 = ["D","E","F"]
let text = zip(arr1, arr2).map{ "\($0.0):\($0.1)" }.joined(separator: ",") //A:D,B:E,C:F
label.text = text
how to display an array on a label
try with this.
you test() function has NSArray return type
func test()-> NSArray{
//bla bla bla
let myArray:NSArray = ["data1","data2","data3"]
print(myArray)
return myArray;
}
then you can get your result
label.text = test().componentsJoinedByString(",")
Displaying Array in Text Label
Use this:
NSArray *listOfWords = @[@"One", @"Two", @"Three"];
NSString * stringToDisplay = [listOfWords componentsJoinedByString:@"\n"];
wordList.text = stringToDisplay;
Will Display:
One
Two
Three
Related Topics
Realm Data Insertion Took Over 7Mins for Big Size JSON
How Pass Data from Button in Tableviewcell to View Controller
Pass Data from Tableviewcontroller to Another Tableviewcontroller in Swift
Swift: How to Continuously Send an Action from a Nstextfield
Osx/Swift: Call a Function When Internet Connection Is Established/Restored
How to Observe Torchlevel in Swift
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property
How to Remove The Fading Animation on .Ondelete Swiftui
Swift: Change Speed of a Moveto Skaction
Nsnumber/Nsdecimalnumber Bizarre Behavior
Mac App Disappears When Window Is Closed Then Another App Is Selected
Customize Mglpolyline Using Mapbox
Filter by Day from Nsdate in Realm, Swift
Mkpolyline Broken When Using Type Satelliteflyover
Realm + Nstableview + Nsarraycontroller
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent
Write Data to Firebase in The Background After Retrieving Steps with Healthkit's Background Delivery