I have a json with key and value as
"average_cost_for_two": 20
When I want to show this in UI, I was show as Avg Cost for two: 20
However I cannot convert the value to String to append "Avg Cost for two" since the value in the Json is an Int.
Basically I want to do append to the published var like this
for i in fetch.nearby_restaurants{
DispatchQueue.main.async {
self.datas.append(datatype(id: i.restaurant.id, name: i.restaurant.name, image: i.restaurant.thumb, rating: "Rating: " + i.restaurant.user_rating.aggregate_rating, cost_for_two: "I want to add my string to show in View here " + i.restaurant.average_cost_for_two, webUrl: i.restaurant.url))
}
}
nearby_restaurants has key as "average_cost_for_two": Int
Any help would be greatly appreciated. Thanks!!
The question is not related to SwiftUI at all.
Basically there are two ways:
Type Conversion: String(i.restaurant.average_cost_for_two)
cost_for_two: "I want to add my string to show in View here " + String(i.restaurant.average_cost_for_two)
String Interpolaction: "\(i.restaurant.average_cost_for_two)"
cost_for_two: "I want to add my string to show in View here \(i.restaurant.average_cost_for_two)"
For more information about String Interpolation please read the Language Guide
EDIT: looking at Swift questions elsewhere, this may not be the best approach, from what I found there's a built in method toString allowing for casting to string. (source: https://stackoverflow.com/a/28203312/2932298) it also seems to vary between versions. Some reference a description property after the int: let x = 10.description, although this is getting language-specific.
Coming from someone who has NO experience with Swift, I'm unsure if I'm able to correctly answer this question or not.
However, as a basic principle most (if not all) languages support int -> string conversion, most data types can be converted to a String very simply.
An idea would be something like: String(average_cost_for_two), converting the int value into a String allowing you to concatenate the values.
I believe the technical term would be type casting a most languages have support for this. Again, especially with X -> string conversions.
Again, no Swift experience, just basic programming principles from the different languages I've developed in.
Related
I have a macOS project that I'm creating UI tests for.
While it's relatively easy to find staticText, buttons, etc. by their text value. Using a subscript lookup on .textViews doesn't (seem to) work.
I've managed to get a reference to the NSTextView I want to inspect using .textViews.firstMatch but I can't figure out how to assert on it's string value.
I'm looking for something that works like this.
XCTAssertEqual(prefs.textViews.firstMatch.stringValue, "Enter text below")
Simply value should do.
It's available on XCUIElementAttributes and is of type Any? that varies based on the type of the element.
XCTAssertEqual(prefs.textViews.firstMatch.value as! String,
"Enter text below")
Ref:
https://developer.apple.com/documentation/xctest/xcuielementattributes
If you print out the debugDescription of the element, you should see which parameter holds the value you want to assert equality on. Likely it will be .value which you can simply coerce into a String for your purposes. Strings adhere to == equality checks, making it trivial to compare two strings with just a simple XCTAssert(originalTextViewValue == "String I want to value check against")
Is there any difference between the following?
var array1_OfStrings = [String]()
var array2_OfStrings: [String] = []
var array3_OfStrings: [String]
Testing in Playground shows that 1 and 2 are the same but 3 behaves differently.
Can someone explain me the difference please? And also what will be the preferred way to declare an empty array of String?
First two have the same effect.
declare a variable array1_OfStrings, let it choose the type itself. When it sees [String](), it smartly knows that's type array of string.
You set the variable array2_OfStrings as type array of string, then you say it's empty by []
This is different because you just tell you want array3_OfStrings to be type array of string, but not given it an initial value.
I think the first one is recommended as The Swift Programming Language uses it more often.
While I might be late to the party, there is one thing that needs to be said.
First option set array1_OfStrings to array of Strings
The other option tells that array1_OfStrings is array of Strings and then set it empty.
While this might be a really small difference, you will notice it while compiling. For the first option compiler will automatically try to find out what is the type of array1_OfStrings. Second option won't do that, you will let compiler know that this actually is array of Strings and done deal.
Why is this important? Take a look at the following link:
https://thatthinginswift.com/debug-long-compile-times-swift/
As you can see, if you don't declare type of your variable that might impact build performance A LOT.
As the title suggests, I'm wondering if anyone out there has any good experience in byte conversion and if there is any good tutorial or information about this.
https://codereview.stackexchange.com/questions/114730/type-to-byte-array-conversion-in-swift
This post explains it very well but if I understand it correctly you can only use this on types like Int, float, Double and so on. I want to learn how to convert any type, like a custom made class to bytes.
How do I convert the string option data type to string in Ocaml?
let function1 data =
match data with
None -> ""
| Some str -> str
Is my implementation error free? Here 'data' has a value of type string option.
To answer your question, yes.
For this simple function, you can easily find it in Option module. For example, Option.default totally fits your purpose:
let get_string data =
Option.default "" data
There are many other useful functions for working with option types in that module, you should check them out to avoid redefine unnecessary functions.
Another point is that the compiler would tell you if there's something wrong. If the compiler doesn't complain, you know that the types all make sense and that you have covered every case in your match expression. The OCaml type system is exceptionally good at finding problems while staying out of your way. Note that you haven't had to define any types yourself in this small example--the compiler will infer that the type of data is string option.
A lot of the problems the compiler can't detect are ones that we can't detect either. We can't tell whether mapping None to the empty string is what you really wanted to do, though it seems very sensible.
In an class header I have seen something like this:
enum {
kAudioSessionProperty_PreferredHardwareSampleRate = 'hwsr', // Float64
kAudioSessionProperty_PreferredHardwareIOBufferDuration = 'iobd' // Float32
};
Now I wonder what data type such an kAudioSessionProperty_PreferredHardwareSampleRate actually is?
I mean this looks like plain old C, but in Objective-C I would write #"hwsr" if I wanted to make it a string.
I want to pass such an "constant" or "enum thing" as argument to an method.
This converts to an UInt32 enum value using the ASCII value of each of the entries. This style have been around for a long time in Mac OS headers.
'hwsr' has the same value as if you had written 0x68777372, but is a lot more reader friendly. If you used the #"hwsr" style instead you would need more than 4 bytes to represent the same.
The advantage of using this style is that you are actually able to quickly identify the content of a raw data stream if you can see the ASCII values of it.