{"id":25453578,"url":"https://github.com/xcessentials/optionalassign","last_synced_at":"2025-05-16T14:35:06.658Z","repository":{"id":63921248,"uuid":"55116622","full_name":"XCEssentials/OptionalAssign","owner":"XCEssentials","description":"Custom assign operator that safely unwraps and assigns non-nil optionals.","archived":false,"fork":false,"pushed_at":"2019-07-20T20:53:14.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-30T18:02:50.572Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XCEssentials.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-31T03:17:19.000Z","updated_at":"2023-12-15T11:51:01.000Z","dependencies_parsed_at":"2023-01-14T14:15:14.836Z","dependency_job_id":null,"html_url":"https://github.com/XCEssentials/OptionalAssign","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XCEssentials%2FOptionalAssign","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XCEssentials%2FOptionalAssign/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XCEssentials%2FOptionalAssign/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XCEssentials%2FOptionalAssign/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XCEssentials","download_url":"https://codeload.github.com/XCEssentials/OptionalAssign/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254546860,"owners_count":22089219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-17T23:56:08.212Z","updated_at":"2025-05-16T14:35:06.610Z","avatar_url":"https://github.com/XCEssentials.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GitHub License](https://img.shields.io/github/license/XCEssentials/OptionalAssign.svg?longCache=true)](LICENSE)\n[![GitHub Tag](https://img.shields.io/github/tag/XCEssentials/OptionalAssign.svg?longCache=true)](https://github.com/XCEssentials/OptionalAssign/tags)\n[![Swift Package Manager Compatible](https://img.shields.io/badge/SPM-compatible-brightgreen.svg?longCache=true)](Package.swift)\n[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-brightgreen.svg?longCache=true)](https://github.com/Carthage/Carthage)\n[![Written in Swift](https://img.shields.io/badge/Swift-5.0-orange.svg?longCache=true)](https://swift.org)\n[![Supported platforms](https://img.shields.io/badge/platforms-macOS%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux-blue.svg?longCache=true)](Package.swift)\n[![Build Status](https://travis-ci.com/XCEssentials/OptionalAssign.svg?branch=master)](https://travis-ci.com/XCEssentials/OptionalAssign)\n\n# OptionalAssign\n\nCustom assign operator that safely unwraps optionals and preserves existing value of the receiver (expression on the left side) unchanged, if the optional on the right side does not have a value (i.e. equal to `nil`).\n\n# When to use\n\nUse this operator in these cases:\n- to safely assign a regular optional value, only if it has non-`nil` value (otherwise operator will do nothing);\n- to safely assign an implicitly unwrapped optional value, only if it has non-`nil` value (otherwise operator will do nothing).\n\n**BONUS**: the value on the right will be automatically converted to receiver type before assign, so no need to cast explicitly.\n\n# How to use\n\nImagine you have a property that is required to always have a value, so you star with default value:\n\n```swift\nvar title = 'Default value'\n```\n... and later you got a dictionary, that might have new value for the `title`:\n\n```swift\nlet aDict: [String: AnyObject] = ... // maybe got from network?\n```\n\nThis is how we can try to get new `title` value from the dictionary using standard Swift syntax:\n\n```swift\nif let newTitleValue = aDict[\"title\"] as? String\n{\n    // yes it has a non-nil value\n    title = newTitleValue\n}\n```\n\nWith optional assign operator you do the same with just this:\n\n```swift\ntitle ?= aDict[\"title\"]\n```\n\nSee unit tests for more examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxcessentials%2Foptionalassign","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxcessentials%2Foptionalassign","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxcessentials%2Foptionalassign/lists"}