-2

So this block of code allows me to initialize a data model

 struct MockMessage: Identifiable {
    let id: Int
    let imageName: String
    let messageText: String
    let isCurrentUser: Bool
}



  let MOCK_MESSAGES: [MockMessage] = [
        .init(id: 0, imageName: "user", messageText: "Hey, what's up?", isCurrentUser: false),
.init(id: iCouldSetANewOne, imageName: iCouldSetANewOne, messageText: iCouldSetANewOne, isCurrentUser: iCouldSetANewOne)]

My question is

1- How can I replicate/initialize attributes instead of manually writing the same attributes over and over again

2- How can I modify it, let's say, the default rotation effect is set to a degree: 25 and I'd like to change it.

Example of Image attributes

 Image(systemName: tabs[index].image)
                                .frame(height: 20)
                                .rotationEffect(.degrees(-25))
                                .foregroundColor(.yellow).scaleEffect(1.25)
2
  • 1) What do you want to accomplish? Can't you create the objects in a loop? 2) How is this related to 1)? Aug 29, 2021 at 15:31
  • 1
    You have asked two pretty much unrelated questions. Ask one question per question. The way I approach 1 is to add a static member to the struct - mockMessages or create a Mock struct with static properties for the various mock data that I need.
    – Paulw11
    Aug 29, 2021 at 20:28

1 Answer 1

1

If I understand correctly, you want to group a commonly used set of modifiers into 1 simple modifier. You can easily do it like this:

extension View {
    func myStyle() -> some View {
        self
            .frame(...)
            .rotationEffect(...)
            .foregroundColor(...)
    }
}

You can even parametrize this function if the values can be different.

About the "changing the rotation effect that is set to 25 degrees" I believe you can have multiple of the same effects, so if you want to rotate some view that is already rotated, just put another .rotationEffect() view modifier on top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.