In my code, I do this:
Text("\(languagesManager.availableWords.count)")
And next time I build, this creates an entry in Localizable.strings: %lld
Is there a way I can flag this UI element to indicate its string doesn't need to be localized?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
It's related to the passByValue nature of structs. In the sample code below, I'm displaying a list of structs (and I can add instances to my list using Int.random(1..<3) to pick one of two possible predefined versions of the struct).
I also have a detail view that can modify the details of a single struct. However when I run this code, it will instead modify all the instances (ie either Sunday or Monday) in my list.
To see this behaviour, run the following code and:
tap New Trigger enough times that there are multiple of at least one of the sunday/monday triggers
tap one of the matching trigger rows
modify either the day, or the int
expected: only one of the rows will reflect the edit
actual: all the matching instances will be updated.
This suggests to me that my Sunday and Monday static instances are being passed by reference when they get added to the array. But I had thought structs were strictly pass by value. What am I missing?
thanks in advance for any wisdom,
Mike
struct ContentView: View {
@State var fetchTriggers: [FetchTrigger] = []
var body: some View {
NavigationView {
VStack {
Button("New Trigger") {
fetchTriggers.append(Int.random(in: 1..<3) == 1 ? .sunMorning : .monEvening)
}
List($fetchTriggers) { fetchTrigger in
NavigationLink(destination: FetchTriggerDetailView(fetchTrigger: fetchTrigger)
.navigationBarTitle("Back", displayMode: .inline))
{
Text(fetchTrigger.wrappedValue.description)
.padding()
}
}
}
}
}
}
struct FetchTrigger: Identifiable {
static let monEvening: FetchTrigger = .init(dayOfWeek: .monday, hour: 6)
static let sunMorning: FetchTrigger = .init(dayOfWeek: .sunday, hour: 3)
let id = UUID()
enum DayOfWeek: Int, Codable, CaseIterable, Identifiable {
var id: Int { self.rawValue }
case sunday = 1
case monday
case tuesday
var description: String {
switch self {
case .sunday: return "Sunday"
case .monday: return "Monday"
case .tuesday: return "Tuesday"
}
}
}
var dayOfWeek: DayOfWeek
var hour: Int
var description: String {
"\(dayOfWeek.description), \(hour):00"
}
}
struct FetchTriggerDetailView: View {
@Binding var fetchTrigger: FetchTrigger
var body: some View {
HStack {
Picker("", selection: $fetchTrigger.dayOfWeek) {
ForEach(FetchTrigger.DayOfWeek.allCases) { dayOfWeek in
Text(dayOfWeek.description)
.tag(dayOfWeek)
}
}
Picker("", selection: $fetchTrigger.hour) {
ForEach(1...12, id: \.self) { number in
Text("\(number)")
.tag(number)
}
}
}
}
}
At the top level, my app has a tab bar.
the second tab's root view is a list view. When a user taps an item in the list, the app presents a detail view for the list item.
The problem I'm currently having is the behaviour the first time the user taps the second tab's button in the tab bar (when running on an iPad in portrait.
There is a navBar at the top, but the screen is otherwise empty. (tapping the left button on the navBar shows the list view, which allows the user to select an item which populates the main detail view)
Is there some way (in swfitUI) to force the list view to show when in portrait? Alternatively/additionally, is there someway to present some instructional view in the otherwise empty view. (It doesn't appear to be creating a standard detail view here until the user exposes the list and picks an item)
Here is some sample code that demonstrates what I'm describing.
thanks in advance for any assistance!
Mike
import SwiftUI
struct ContentView: View {
var body: some View {
TabView(){
FirstTabView()
.tabItem {
Text("First")
}
.tag(0)
SecondTabView()
.tabItem {
Text("Second")
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice(PreviewDevice(rawValue: "iPad8,1"))
}
}
struct FirstTabView: View {
var body: some View {
Text("First View")
}
}
struct SecondTabView: View {
var body: some View {
NavigationView {
NavigationLink(destination: Text("Detail View")) {
Text("SummaryView")
}
.navigationBarTitle("Navigation")
}
}
}
I seem to recall hearing that DateFormatters are (or were) expensive to instantiate.
With this in mind, I tried a small experiment with the following code:
class MyClass {
		static let df = DrawingCellView.dateFormatter
static var dateFormatter: DateFormatter {
let result = DateFormatter()
result.dateFormat = "yyyy-MM-dd"
return result
}
		func dateText() -> String {
				return MyClass.dateFormatter.string(from: Date())
		}
When I put a breakpoint in the dateFormatter code, it fires each time I use it. However if I instead use df, the breakpoint only fires once.
Does this make sense?
If so, is this the recommended way to construct a runOnce static constant declaration/assignment?
thanks!
Mike
I have a SwiftUI view that works as expected in a full SwiftUI context. But I now need to use it in a UIViewController. It mostly works, but I'm trying to expose an @State var out to the viewController, and it only ever returns the initial value.
Any guidance for how best to pass out this @State var?
I could make it be a binding, but in my pure SwiftUI code, it works fine as @State (ie EditorView's container view does not need to know about sliderVal)
thanks, in advance, for any suggestions
import SwiftUI
import UIKit
class ViewController: UIViewController {
var host: UIHostingController<EditorView>?
override func viewDidLoad() {
super.viewDidLoad()
host = .init(rootView: EditorView())
guard let host = host else { return }
addChild(host)
host.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(host.view)
host.didMove(toParent: self)
}
@IBAction func helloTapped(sender: UIButton) {
guard let sliderValue = host?.rootView.sliderVal else { return }
print("UIKit sliderValue: \(sliderValue)")
}
}
struct EditorView: View {
@State var sliderVal: Double
init(sliderVal: Double? = nil) {
_sliderVal = State(initialValue: sliderVal ?? 7)
}
var body: some View {
VStack {
Slider(value: $sliderVal, in: 1...10)
Text("sliderVal: \(sliderVal)")
}
}
}
(NOTE: in order to see this code snippet in action you will need to create a button in the storyboard and link it to helloTapped)
This first code works fine decoding json.
static let localizationsDictText = """
{ "en" : { "string" : "Cat" },
"fr" : { "string" : "Chat"}
}
"""
struct Localization: Codable, Equatable {
let string: String
}
typealias LocalizationsDict = [String: Localization]
func testExample() throws {
let text = Self.localizationsDictText
let data = text.data(using: .utf8, allowLossyConversion: false)
let localizationsDict = try JSONDecoder().decode(LocalizationsDict.self, from: data!)
XCTAssertEqual(localizationsDict.keys.count, 2)
}
But then I try to create a modified version of the above, only changing the type of the LocalizationsDict key from String to an enum:
enum Language: String, CaseIterable, Codable {
case en = "en"
case fr = "fr"
}
typealias LocalizationsDict2 = [Language: Localization]
func testExample2() throws {
let text = Self.localizationsDictText
let data = text.data(using: .utf8, allowLossyConversion: false)
let localizationsDict = try JSONDecoder().decode(LocalizationsDict2.self, from: data!)
XCTAssertEqual(localizationsDict.keys.count, 2)
}
and the JSONDecoder line throws an exception:
testExample2(): failed: caught error: "typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))"
Any suggestions as to why switching from
[String: Localization] to [Language: Localization] might cause JSONDecode() to treat it like an array?
Thanks in advance for any guidance.
I am using @AppStorage in a model object (see code below that works as expected).
class ModelObject {
static let shared = ModelObject()
@AppStorage("enhanced") var scriptPickers: Bool = true
var defaultDependentValue: String {
scriptPickers ? "Enhanced" : "NOT enhanced"
}
}
struct ContentView: View {
@AppStorage("enhanced") var scriptPickers: Bool = true
var body: some View {
VStack {
Toggle(isOn: $scriptPickers, label: {
Text("userDefault val")
})
Text("value: \(ModelObject.shared.defaultDependentValue)")
}
}
}
Now I want to test my model object in a way that will allow me to use a mock instance of UserDefaults, but am having trouble with the syntax. I tried adding a userDefaults var, and referring to the var in the @AppStorage
class ModelObject {
static let shared = ModelObject()
let userDefaults: UserDefaults
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
@AppStorage("enhanced", store: userDefaults) var scriptPickers: Bool = true
var defaultDependentValue: String {
scriptPickers ? "Enhanced" : "NOT enhanced"
}
}
However I can't find a way to avoid the syntax error this generates:
Cannot use instance member 'userDefaults' within property initializer; property initializers run before 'self' is available
Any guidance on how I might be able to:
continue using @AppStorage
be able to test my class in a way that doesn't force me to use UserDefaults.standard
thanks, in advance,
Mike
From what I've read, @AppStorage vars should be @Published, however the following code generates a syntax error at extended's .sink modifier: Cannot call value of non-function type 'Binding<Subject>'
class LanguageManager: ObservableObject {
@Published var fred = "Fred"
@AppStorage("extended") var extended: Bool = true
private var subscriptions = Set<AnyCancellable>()
init() {
$fred
.sink(receiveValue: {value in
print("value: \(value)")
})
.store(in: &subscriptions)
$extended
.sink(receiveValue: {value in
print("value: \(value)")
})
.store(in: &subscriptions)
}
Does anyone know of a way to listen for (subscribe to) changes in @AppStorage values?
didSet works in for a specific subset of value changes, but this is not sufficient for my intended use.
Sorry if this question is too vague, however I've tried this multiple times and see the same result. I'm pretty sure I'm doing something wrong, but don't know what.
I have a Multiplatform (iOS and macOS) project that builds, and runs
I add a new target of type Unit Test Bundle
I click the diamond in the margin beside the XCTestCase declaration at the top of the new Test file
The target project builds, then Xcode says 'Testing...' and it stays like this forever.
I've also tried creating a new scheme that targets the target created in the above steps. attempting to run my tests behaves the same. The top status bar will get stuck saying 'Testing...' and never get anywhere.
I'm pretty sure this is something basic.
thanks, in advance for any guidance.
Mike
Pretty sure this is a no-no, but asking just in case there's an easy way to make this work
struct DocumentContentView: View {
private static let logger = Logger(
subsystem: "mySubsystem",
category: String(describing: Self.self)
)
var body: some View {
VStack {
Text("Hello")
logger.trace("hello")
}
}
}
This code generates the following compile error at the logger.trace line
buildExpression is unavailable: this expression does not conform to View
I suspect every line of the body var (or any @ViewBuilder - designated code?) needs to 'return' a View. Is this correct? or more importantly any work arounds other than putting some/all of the view contents in a. func()?
I must be missing something here. I want to put a landscape image in a geometry reader that contains a ZStack that contains an image and an overlay centred on top of the Image.
I would like the ZStack and GeoReader's sizes to be the size of Image. (ie I want geometry.size to be the size of the image, which can be used to control the offset of the overlay's position.)
Unfortunately the ZStack also includes the space above the image (ie the top safeArea) and the GeometryReader also includes all the space below the Image. (so geometry.size.height is greater than the height of Image)
I've gone down rabbit holes of adding other items above/below, but I don't seem to be able to prevent the GeometryReader from being vertically greedy.
eg the Text(" ") above the ZStack in the VStack solves the ZStack claiming the top safe area. But adding Text(" ") below the ZStack does not prevent the GeometryReader from claiming more vertical space below the image.
Any/all guidance greatly appreciated.
struct ContentView: View {
var body: some View {
VStack {
// Text(" ")
GeometryReader { geometry in
ZStack {
Image(
uiImage: .init(imageLiteralResourceName: "LandscapeSample")
)
.resizable()
.aspectRatio(contentMode: .fit)
Text("Hello, world!")
.background(.white)
}
.background(.red)
}
.background(.blue)
// Text(" ")
}
}
}
When I run this in a playground:
var meDate = Calendar.current.date(from: DateComponents(year: 2024, hour: 7, weekday: 3, weekdayOrdinal: 2))!
print(meDate)
I see:
2024-01-09 15:00:00 +0000
This seems correct to me.
jan 9th is the second Tuesday in 2024
I'm in the pacific TZ, 07:00 PDT matches 15:00GMT
But then I do this:
meDate = Calendar.current.date(bySetting: .weekday, value: 4, of: meDate)!
print(meDate)
and I see: 2024-01-10 08:00:00 +0000
I would have expected my hour value (7PST/15GMT) to have been preserved. Is there a way I can update weekday, but not lose my hour?
I'm defining a typealias for a set, and then creating an extension for the new typealias.
When I do this, I'm getting an odd syntax error. Any/all guidance appreciated.
typealias IntSet = Set<Int>
extension IntSet {
func aFunction() -> Set<String> {
let array: [String] = self.map { "\($0)" }
return Set(array)
}
}
At the return line, I get the following syntax error: Cannot convert return expression of type 'Set<Int>' to return type 'Set<String>'
Even if I replace the return line with the following, I get the same compile error
return Set("array")
I have a singleton instance of a class that (among other things) is managing which subset of words will be available to users.
The contents of availableWords will always be a subset of words and is always a function of three userDefaults that are bound to user settings (using @AppStorage)
I could dynamically reconstruct availableWords every time it is needed, but it will be read much more frequently than it changes. Because of this, I want to cache the updated list every time a user changes one of the settings that will change its contents.
But the only way I can see to do this is to create an update function and rely on the UI code to call the function any time a user updates one of the settings that will require availableWords to be updated. And this feels more like something out of UIKit.
Do any of you see a better way of managing the updates of availableWords?
class WordsManager {
static let shared = WordsManager()
let words: Words // defined in init
var availableWords: Words // updated anytime scriptPickers, languageChoice or cardPile changes
@AppStorage("scriptPickers") var scriptPickers: ScriptPickers = ScriptPickers.defaultDictionary
@AppStorage("languageChoice") var languageChoice: LanguageChoice = .all
@AppStorage("cardPile") var cardPile: CardPile = .allRandom
func updateAvailableWords() {
var result = words.filtered(by: cardPile.wordList)
let askIdentifiers = languageChoice.askLanguages
let answerIdentifiers = languageChoice.answerLanguages
result = result.matching(askIdentifiers: askIdentifiers, answerIdentifiers: answerIdentifiers)
self.availableWords = result
}
// other stuff
}
I've defined a value stored in UserDefaults.
In a view struct I have code that can successfully update the stored value.
I've also added an @AppStorage var in an instance of a class, that can read this value and run business logic that depends on the current stored value.
But what I really want to do, is have code in my class that gets automatically called when the value stored in UserDefaults gets updated.
Basically I want to do this:
@AppStorage("languageChoice") var languageChoice: LanguageChoice = .all {
didSet {
print("hello")
}
}
Unfortunately didSet closures in @AppStorage vars do not appear to get called :-(
My clumsy attempts to use combine have all ended in tears from the compiler.
Any/all suggestions are greatly appreciated.
thanks,
Mike