{"id":20440368,"url":"https://github.com/smakeev/swiftanonymousclass","last_synced_at":"2025-04-12T23:09:28.387Z","repository":{"id":62456444,"uuid":"209751117","full_name":"smakeev/SwiftAnonymousClass","owner":"smakeev","description":"Analog of anonymous class in Swift.","archived":false,"fork":false,"pushed_at":"2019-09-25T08:08:37.000Z","size":73,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T23:09:18.929Z","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/smakeev.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":"2019-09-20T09:13:30.000Z","updated_at":"2020-11-25T12:22:37.000Z","dependencies_parsed_at":"2022-11-02T01:01:40.519Z","dependency_job_id":null,"html_url":"https://github.com/smakeev/SwiftAnonymousClass","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smakeev%2FSwiftAnonymousClass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smakeev%2FSwiftAnonymousClass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smakeev%2FSwiftAnonymousClass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smakeev%2FSwiftAnonymousClass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smakeev","download_url":"https://codeload.github.com/smakeev/SwiftAnonymousClass/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643004,"owners_count":21138355,"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":"2024-11-15T09:23:51.036Z","updated_at":"2025-04-12T23:09:28.367Z","avatar_url":"https://github.com/smakeev.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftAnonymousClass\n\nAnalog of anonymous class in Swift.\n\nSwiftAnonymousClass adds function ```_new``` in your project.\n```swift\npublic func _new\u003cType\u003e(owner: AnyObject? = nil, _ objectCreator:()-\u003eType) -\u003e Type\n```\nLet's say you have a protocol:\n```swift\nprotocol Greeting {\n    func sayHello() -\u003e String\n}\n```\nYou need a class wich says hello.\n\n```swift\nlet object: Greeting = _new {\n  class NoName: Greeting {\n    func sayHello() -\u003e String {\n      return \"hello from no name class\"\n    }\n  }\n  return NoName()\n}\n\nobject.sayHello()\n```\nMore real example:\n\n```swift\n        let configuration = URLSessionConfiguration.default\n        var downloadsSession = URLSession(configuration: configuration,\n                                          delegate: _new {\n                                            class NoName: NSObject, URLSessionDownloadDelegate {\n                                                func urlSession(_ session: URLSession,\n                                                                downloadTask: URLSessionDownloadTask,\n                                                                didFinishDownloadingTo location: URL) {\n                                                    print(\"Finished downloading to \\(location).\")\n                                                }\n                                            }\n                                            return NoName()\n                                          },\n                                          delegateQueue: nil)\n```                                          \nNote URLSession stores it's delegate strongly but, usually delegates are stored weakly. So you will get nil if just use ```some.delegate = _new {...}```.\nFirstly you need to store it strongly.\nTo do this you may use ```owner``` parameter of ```_new``` function.\nThis parameter will bind lifetime of ```_new``` object and ```owner```\nnew anonimous object will be removed on deinit of owner.\n\nexample:\n```swift\n\nprotocol ADelegate: class {\n\tfunc `do`()\n }\n \n class A {\n\tweak var delegate: ADelegate?\n\t\n\tdeinit {\n\t\tdelegate?.do()\n\t}\n}\n\n...\n\nlet a = A()\na.delegate = _new(owner: a) {\t\t\t\n\t\t\tclass ADelegateInstance: ADelegate {\n\t\t\t\n\t\t\t\tfunc `do`() {\n\t\t\t\t\tprint(\"DO from delegate\")\n\t\t\t\t}\n\t\t\t\treturn ADelegateInstance()\n\t\t\t}\n      }\n```\n\nIf you set new anonymous delegate the first will be deallocated.\nIf you set new delegate (not from _new) your anonymous delegate will stay untill owner deallocated.\nTo make it be removed forced you may first set _new with nil. Example:\n\nLet's say you have Some class with week delegate ```SomeDelegate```. \nYou have some class ```SOmeDelegateInstance``` and it's instance stored strongly somewhere.\nFirstly you set ```Some``` instance delegate as an anonymous object.\nBut after you change it to not anonymous class your anonymous class will be unavailable but alive.\nTo make it be destoyed before some instance be released you may do the follow:\n\n```swift\n\nsome.delegate = _new(owner:some) {\n    class NoName: SomeDelegate {\n        func `do`() {\n            print(\"DO\")\n        }\n    }\n    return NoName()\n}\n\nsome.delegate = _new(owner:some) { nil }\nsome.delegate = someDelegateInstance\n```\n\n## How to use:\nYou may copy SwiftAnonymousClass.swift directly to your project,\nor use cocoa pods:\n```\npod \"SwiftAnonymousClass\"\n```\nyou will need to add \n```swift\nimport SwiftAnonymousClass\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakeev%2Fswiftanonymousclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmakeev%2Fswiftanonymousclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakeev%2Fswiftanonymousclass/lists"}