Tagged "programmierung"

Scala Request Sttp

It took me an entire workday to figure out how to make an API request to a server that responds with Content-Encoding: identity using the STTP client.

It seems that the STTP client always tries to decompress the server’s response.
However, the backend (in this case, a Microsoft API) ignores the Accept-Encoding settings in the header.

The solution to this issue was to add .disableAutoDecompression to the client settings.

You can find the final working code snippet below:

ScrollView Scroll To

Below you will find a little code snippet to scroll a ScrollView to a defined position.

ScrollViewReader { reader in
	ScrollView {
		ForEach(messages) { message in
			MessageBubble(message: message.message, isFromCurrentUser: message.fromUser)
			.id(message.id)
		}
	}
	.onChange(of: messages.count) {
		withAnimation {
			reader.scrollTo(messages.last?.id, anchor: .top)
		}
	}
	.padding()
}

UIView Farbverlauf

Möchte man einen UIView mit einem Farbverlauf versehen, kann man dazu ganz einfach CAGradientLayer() verwenden. Dieser Layer wird dann als sublayer hinzugefügt.

Ich machte anfänglich den Fehler und versuchte den Layer in awaikFromNib zu setzen. Hierbei wurde er mir nicht angezeigt. Der Grund dafür ist, dass hier die Inhalte noch nicht gefüllt und damit die Höhe und Breite des Views noch 0 waren. Der Frame des Layers ändert sich nämlich nicht einfach mit. Darum das ganz nun im “draw”:

Localized errors in swift

I created a small gist on github.com to show you how to create your own custom errors in swift including localized descriptions. The script is mostly self describing. Feel free to leave a comment if not.

// sepcifiy your errors
enum MyCustomError : Error {
    case customError
    case missingProperty

    var localizedDescription: String {
        switch self {
        case .urlCouldNotBeCreated:
            return NSLocalizedString("MyCustomErrorCcustomError", value: "a localized error message", comment: "")
        case .missingProperty:
            return NSLocalizedString("MyCustomErrorMissingProperty", value: "Missing coordinates", comment: "")
        }
    }
}

// use your error
func demo() throws {
    throw MyCustomError.customError
}

// catch your error
do {
    try demo()
} catch let error as MyCustomError {
    print(error.localizedDescription)
}