├── README.md
├── SwiftMulticastProtocol.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ │ └── frind.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
└── xcuserdata
│ └── frind.xcuserdatad
│ ├── xcdebugger
│ └── Breakpoints_v2.xcbkptlist
│ └── xcschemes
│ ├── SwiftMulticastProtocol.xcscheme
│ └── xcschememanagement.plist
└── SwiftMulticastProtocol
├── AppDelegate.swift
├── Assets.xcassets
├── AppIcon.appiconset
│ ├── Contents.json
│ ├── iPhone_App_iOS7-10_60pt@2x.png
│ ├── iPhone_App_iOS7-10_60pt@3x.png
│ ├── iPhone_Notifications_iOS7-10_20pt@2x.png
│ ├── iPhone_Notifications_iOS7-10_20pt@3x.png
│ ├── iPhone_Settings_iOS5-10_29pt@2x.png
│ ├── iPhone_Settings_iOS5-10_29pt@3x.png
│ ├── iPhone_Spotlight_iOS7-10_40pt@2x.png
│ └── iPhone_Spotlight_iOS7-10_40pt@3x.png
├── Contents.json
├── first.imageset
│ ├── Contents.json
│ └── first.pdf
├── logo.imageset
│ ├── 28453311-1919a444-6df7-11e7-9ffd-e4438c01c903.png
│ └── Contents.json
└── second.imageset
│ ├── Contents.json
│ └── second.pdf
├── Base.lproj
├── LaunchScreen.storyboard
└── Main.storyboard
├── FirstViewController.swift
├── Info.plist
├── SecondViewController.swift
├── SwiftMulticastProtocol.swift
└── ThirdViewController.swift
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # SwiftMulticastProtocol
3 | Easy way to use a multicast intefrace
4 |
5 |
6 |
7 |
8 | []()
9 | []()
10 |
11 | ★★ Star our github repository to help us!, or ☕ pay me a coffee ★★
12 | Created by Luca Becchetti
13 |
14 | With this simple method you can implement a multicast interface to send a message from one class to all registered instances.
15 |
16 | ## You also may like
17 |
18 | Do you like `SwiftMultiSelect`? I'm also working on several other opensource libraries.
19 |
20 | Take a look here:
21 |
22 | * **[InAppNotify](https://github.com/lucabecchetti/InAppNotify)** - Manage in app notifications
23 | * **[CountriesViewController](https://github.com/lucabecchetti/CountriesViewController)** - Countries selection view
24 | * **[SwiftMultiSelect](https://github.com/lucabecchetti/SwiftMultiSelect)** - Generic multi selection tableview
25 |
26 | ## Demo
27 |
28 | 
29 |
30 | For example, to create a simple chat application, you have a UITableView with a list of chats, and you have another class called ChatActivity with a UICollectionView of messages. Both class want to know when new message has been received or delivered. First class to show a badge count, second class to show bubble message. First of all you have to create your interface, for example:
31 |
32 | ```swift
33 | import Foundation
34 |
35 | /// Custom type, edit as you need
36 | typealias ChatMessage = String
37 |
38 | /// Delegate to implement in all classes
39 | @objc protocol MessageDelegate {
40 |
41 | ///Called when new message has been received
42 | func newMessageReceived(message: ChatMessage)
43 |
44 | }
45 | ```
46 |
47 | To be able to have a multiple connection to this protocol, create a multicast delegate like this:
48 |
49 | ```swift
50 | /// Delegate multicast
51 | class MessageDelegateMulticast {
52 |
53 | //Array of registered classes
54 | private var notifiers: [MessageDelegate] = []
55 |
56 |
57 | /// New message received method
58 | ///
59 | /// - Parameter message: custom message type
60 | func newMessageReceived(message: ChatMessage){
61 | //Notify to all classes
62 | notifyAll { notifier in
63 | notifier.newMessageReceived(message: message)
64 | }
65 | }
66 |
67 |
68 | /// Method Register class from multicast, usually called in ViewDidLoad
69 | ///
70 | /// - Parameter notifier: class that implement MessageDelegate
71 | func addNotifier(notifier: MessageDelegate) {
72 | removeNotifier(notifier: notifier)
73 | notifiers.append(notifier)
74 | }
75 |
76 | /// Method to unregister class from multicast, usually called in ViewDidDisappear
77 | ///
78 | /// - Parameter notifier: class that implement MessageDelegate
79 | func removeNotifier(notifier: MessageDelegate) {
80 | for i in 0 ..< notifiers.count {
81 | if notifiers[i] === notifier || object_getClassName(notifiers[i]) == object_getClassName(notifier) {
82 | notifiers.remove(at: i)
83 | break;
84 | }
85 | }
86 | }
87 |
88 |
89 | /// Method that notify to all registered classes
90 | ///
91 | /// - Parameter notify: class that implement MessageDelegate
92 | private func notifyAll(notify: (MessageDelegate) -> ()) {
93 | for notifier in notifiers {
94 | notify(notifier)
95 | }
96 | }
97 |
98 | }
99 |
100 | /// Singleton class for multicast
101 | let messageDelegateMulticast = MessageDelegateMulticast()
102 | ```
103 |
104 | ## USAGE
105 | Each class, to receive the message can subscribe to a delegate:
106 |
107 | ```swift
108 | class ClientA:MessageDelegate{
109 |
110 | override func viewDidLoad() {
111 | messageDelegateMulticast.addNotifier(self)
112 | }
113 |
114 | override func viewDidDisappear(animated: Bool) {
115 | messageDelegateMulticast.removeNotifier(self)
116 | }
117 |
118 | //MARK
119 | func newMessageReceived(message: ChatMessage){
120 | print("Message received from A: \(message)")
121 | }
122 |
123 | }
124 |
125 | class ClientB:MessageDelegate{
126 |
127 | override func viewDidLoad() {
128 | messageDelegateMulticast.addNotifier(self)
129 | }
130 |
131 | override func viewDidDisappear(animated: Bool) {
132 | messageDelegateMulticast.removeNotifier(self)
133 | }
134 |
135 | //MARK
136 | func newMessageReceived(message: ChatMessage){
137 | print("Message received from B: \(message)")
138 | }
139 |
140 | }
141 | ```
142 |
143 | Now when method of delegate is called like this:
144 |
145 | ```swift
146 | messageDelegateMulticast.newMessageReceived(message)
147 | ```
148 |
149 | ClassA and ClassB will be informed and will receive the message.
150 |
151 | ## Projects using SwiftMulticastProtocol
152 |
153 | - Frind - [www.frind.it](https://www.frind.it)
154 |
155 | ### Your App and SwiftMulticastProtocol
156 | I'm interested in making a list of all projects which use this library. Feel free to open an Issue on GitHub with the name and links of your project; we'll add it to this site.
157 |
158 | ## Credits & License
159 | SwiftMulticastProtocol is owned and maintained by [Luca Becchetti](http://www.lucabecchetti.com)
160 |
161 | As open source creation any help is welcome!
162 |
163 | The code of this library is is free to use and change (I'm happy to discuss any PR with you)
164 |
165 | ## About me
166 |
167 | I am a professional programmer with a background in software design and development, currently developing my qualitative skills on a startup company named "[Frind](https://www.frind.it) " as Project Manager and ios senior software engineer.
168 |
169 | I'm high skilled in Software Design (10+ years of experience), i have been worked since i was young as webmaster, and i'm a senior Php developer. In the last years i have been worked hard with mobile application programming, Swift for ios world, and Java for Android world.
170 |
171 | I'm an expert mobile developer and architect with several years of experience of team managing, design and development on all the major mobile platforms: iOS, Android (3+ years of experience).
172 |
173 | I'm also has broad experience on Web design and development both on client and server side and API /Networking design.
174 |
175 | All my last works are hosted on AWS Amazon cloud, i'm able to configure a netowrk, with Unix servers. For my last works i configured apache2, ssl, ejabberd in cluster mode, Api servers with load balancer, and more.
176 |
177 | I live in Assisi (Perugia), a small town in Italy, for any question, [contact me](mailto:luca.becchetti@brokenice.it)
178 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | B1951C511F21EAFB001EDB92 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1951C501F21EAFB001EDB92 /* AppDelegate.swift */; };
11 | B1951C531F21EAFB001EDB92 /* FirstViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1951C521F21EAFB001EDB92 /* FirstViewController.swift */; };
12 | B1951C551F21EAFB001EDB92 /* SecondViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1951C541F21EAFB001EDB92 /* SecondViewController.swift */; };
13 | B1951C581F21EAFB001EDB92 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B1951C561F21EAFB001EDB92 /* Main.storyboard */; };
14 | B1951C5A1F21EAFB001EDB92 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B1951C591F21EAFB001EDB92 /* Assets.xcassets */; };
15 | B1951C5D1F21EAFB001EDB92 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B1951C5B1F21EAFB001EDB92 /* LaunchScreen.storyboard */; };
16 | B1951C651F21EBCB001EDB92 /* ThirdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1951C641F21EBCB001EDB92 /* ThirdViewController.swift */; };
17 | B1951C671F21EC19001EDB92 /* SwiftMulticastProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1951C661F21EC19001EDB92 /* SwiftMulticastProtocol.swift */; };
18 | /* End PBXBuildFile section */
19 |
20 | /* Begin PBXFileReference section */
21 | B1951C4D1F21EAFB001EDB92 /* SwiftMulticastProtocol.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftMulticastProtocol.app; sourceTree = BUILT_PRODUCTS_DIR; };
22 | B1951C501F21EAFB001EDB92 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
23 | B1951C521F21EAFB001EDB92 /* FirstViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstViewController.swift; sourceTree = ""; };
24 | B1951C541F21EAFB001EDB92 /* SecondViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondViewController.swift; sourceTree = ""; };
25 | B1951C571F21EAFB001EDB92 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
26 | B1951C591F21EAFB001EDB92 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
27 | B1951C5C1F21EAFB001EDB92 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
28 | B1951C5E1F21EAFB001EDB92 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
29 | B1951C641F21EBCB001EDB92 /* ThirdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThirdViewController.swift; sourceTree = ""; };
30 | B1951C661F21EC19001EDB92 /* SwiftMulticastProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftMulticastProtocol.swift; sourceTree = ""; };
31 | /* End PBXFileReference section */
32 |
33 | /* Begin PBXFrameworksBuildPhase section */
34 | B1951C4A1F21EAFB001EDB92 /* Frameworks */ = {
35 | isa = PBXFrameworksBuildPhase;
36 | buildActionMask = 2147483647;
37 | files = (
38 | );
39 | runOnlyForDeploymentPostprocessing = 0;
40 | };
41 | /* End PBXFrameworksBuildPhase section */
42 |
43 | /* Begin PBXGroup section */
44 | B1951C441F21EAFB001EDB92 = {
45 | isa = PBXGroup;
46 | children = (
47 | B1951C4F1F21EAFB001EDB92 /* SwiftMulticastProtocol */,
48 | B1951C4E1F21EAFB001EDB92 /* Products */,
49 | );
50 | sourceTree = "";
51 | };
52 | B1951C4E1F21EAFB001EDB92 /* Products */ = {
53 | isa = PBXGroup;
54 | children = (
55 | B1951C4D1F21EAFB001EDB92 /* SwiftMulticastProtocol.app */,
56 | );
57 | name = Products;
58 | sourceTree = "";
59 | };
60 | B1951C4F1F21EAFB001EDB92 /* SwiftMulticastProtocol */ = {
61 | isa = PBXGroup;
62 | children = (
63 | B1951C501F21EAFB001EDB92 /* AppDelegate.swift */,
64 | B1951C521F21EAFB001EDB92 /* FirstViewController.swift */,
65 | B1951C541F21EAFB001EDB92 /* SecondViewController.swift */,
66 | B1951C561F21EAFB001EDB92 /* Main.storyboard */,
67 | B1951C591F21EAFB001EDB92 /* Assets.xcassets */,
68 | B1951C5B1F21EAFB001EDB92 /* LaunchScreen.storyboard */,
69 | B1951C5E1F21EAFB001EDB92 /* Info.plist */,
70 | B1951C641F21EBCB001EDB92 /* ThirdViewController.swift */,
71 | B1951C661F21EC19001EDB92 /* SwiftMulticastProtocol.swift */,
72 | );
73 | path = SwiftMulticastProtocol;
74 | sourceTree = "";
75 | };
76 | /* End PBXGroup section */
77 |
78 | /* Begin PBXNativeTarget section */
79 | B1951C4C1F21EAFB001EDB92 /* SwiftMulticastProtocol */ = {
80 | isa = PBXNativeTarget;
81 | buildConfigurationList = B1951C611F21EAFB001EDB92 /* Build configuration list for PBXNativeTarget "SwiftMulticastProtocol" */;
82 | buildPhases = (
83 | B1951C491F21EAFB001EDB92 /* Sources */,
84 | B1951C4A1F21EAFB001EDB92 /* Frameworks */,
85 | B1951C4B1F21EAFB001EDB92 /* Resources */,
86 | );
87 | buildRules = (
88 | );
89 | dependencies = (
90 | );
91 | name = SwiftMulticastProtocol;
92 | productName = SwiftMulticastProtocol;
93 | productReference = B1951C4D1F21EAFB001EDB92 /* SwiftMulticastProtocol.app */;
94 | productType = "com.apple.product-type.application";
95 | };
96 | /* End PBXNativeTarget section */
97 |
98 | /* Begin PBXProject section */
99 | B1951C451F21EAFB001EDB92 /* Project object */ = {
100 | isa = PBXProject;
101 | attributes = {
102 | LastSwiftUpdateCheck = 0830;
103 | LastUpgradeCheck = 0830;
104 | ORGANIZATIONNAME = "Luca Becchetti";
105 | TargetAttributes = {
106 | B1951C4C1F21EAFB001EDB92 = {
107 | CreatedOnToolsVersion = 8.3.2;
108 | DevelopmentTeam = 49RE3G6CPG;
109 | ProvisioningStyle = Automatic;
110 | };
111 | };
112 | };
113 | buildConfigurationList = B1951C481F21EAFB001EDB92 /* Build configuration list for PBXProject "SwiftMulticastProtocol" */;
114 | compatibilityVersion = "Xcode 3.2";
115 | developmentRegion = English;
116 | hasScannedForEncodings = 0;
117 | knownRegions = (
118 | en,
119 | Base,
120 | );
121 | mainGroup = B1951C441F21EAFB001EDB92;
122 | productRefGroup = B1951C4E1F21EAFB001EDB92 /* Products */;
123 | projectDirPath = "";
124 | projectRoot = "";
125 | targets = (
126 | B1951C4C1F21EAFB001EDB92 /* SwiftMulticastProtocol */,
127 | );
128 | };
129 | /* End PBXProject section */
130 |
131 | /* Begin PBXResourcesBuildPhase section */
132 | B1951C4B1F21EAFB001EDB92 /* Resources */ = {
133 | isa = PBXResourcesBuildPhase;
134 | buildActionMask = 2147483647;
135 | files = (
136 | B1951C5D1F21EAFB001EDB92 /* LaunchScreen.storyboard in Resources */,
137 | B1951C5A1F21EAFB001EDB92 /* Assets.xcassets in Resources */,
138 | B1951C581F21EAFB001EDB92 /* Main.storyboard in Resources */,
139 | );
140 | runOnlyForDeploymentPostprocessing = 0;
141 | };
142 | /* End PBXResourcesBuildPhase section */
143 |
144 | /* Begin PBXSourcesBuildPhase section */
145 | B1951C491F21EAFB001EDB92 /* Sources */ = {
146 | isa = PBXSourcesBuildPhase;
147 | buildActionMask = 2147483647;
148 | files = (
149 | B1951C671F21EC19001EDB92 /* SwiftMulticastProtocol.swift in Sources */,
150 | B1951C551F21EAFB001EDB92 /* SecondViewController.swift in Sources */,
151 | B1951C511F21EAFB001EDB92 /* AppDelegate.swift in Sources */,
152 | B1951C651F21EBCB001EDB92 /* ThirdViewController.swift in Sources */,
153 | B1951C531F21EAFB001EDB92 /* FirstViewController.swift in Sources */,
154 | );
155 | runOnlyForDeploymentPostprocessing = 0;
156 | };
157 | /* End PBXSourcesBuildPhase section */
158 |
159 | /* Begin PBXVariantGroup section */
160 | B1951C561F21EAFB001EDB92 /* Main.storyboard */ = {
161 | isa = PBXVariantGroup;
162 | children = (
163 | B1951C571F21EAFB001EDB92 /* Base */,
164 | );
165 | name = Main.storyboard;
166 | sourceTree = "";
167 | };
168 | B1951C5B1F21EAFB001EDB92 /* LaunchScreen.storyboard */ = {
169 | isa = PBXVariantGroup;
170 | children = (
171 | B1951C5C1F21EAFB001EDB92 /* Base */,
172 | );
173 | name = LaunchScreen.storyboard;
174 | sourceTree = "";
175 | };
176 | /* End PBXVariantGroup section */
177 |
178 | /* Begin XCBuildConfiguration section */
179 | B1951C5F1F21EAFB001EDB92 /* Debug */ = {
180 | isa = XCBuildConfiguration;
181 | buildSettings = {
182 | ALWAYS_SEARCH_USER_PATHS = NO;
183 | CLANG_ANALYZER_NONNULL = YES;
184 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
185 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
186 | CLANG_CXX_LIBRARY = "libc++";
187 | CLANG_ENABLE_MODULES = YES;
188 | CLANG_ENABLE_OBJC_ARC = YES;
189 | CLANG_WARN_BOOL_CONVERSION = YES;
190 | CLANG_WARN_CONSTANT_CONVERSION = YES;
191 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
192 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
193 | CLANG_WARN_EMPTY_BODY = YES;
194 | CLANG_WARN_ENUM_CONVERSION = YES;
195 | CLANG_WARN_INFINITE_RECURSION = YES;
196 | CLANG_WARN_INT_CONVERSION = YES;
197 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
198 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
199 | CLANG_WARN_UNREACHABLE_CODE = YES;
200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
201 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
202 | COPY_PHASE_STRIP = NO;
203 | DEBUG_INFORMATION_FORMAT = dwarf;
204 | ENABLE_STRICT_OBJC_MSGSEND = YES;
205 | ENABLE_TESTABILITY = YES;
206 | GCC_C_LANGUAGE_STANDARD = gnu99;
207 | GCC_DYNAMIC_NO_PIC = NO;
208 | GCC_NO_COMMON_BLOCKS = YES;
209 | GCC_OPTIMIZATION_LEVEL = 0;
210 | GCC_PREPROCESSOR_DEFINITIONS = (
211 | "DEBUG=1",
212 | "$(inherited)",
213 | );
214 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
215 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
216 | GCC_WARN_UNDECLARED_SELECTOR = YES;
217 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
218 | GCC_WARN_UNUSED_FUNCTION = YES;
219 | GCC_WARN_UNUSED_VARIABLE = YES;
220 | IPHONEOS_DEPLOYMENT_TARGET = 10.3;
221 | MTL_ENABLE_DEBUG_INFO = YES;
222 | ONLY_ACTIVE_ARCH = YES;
223 | SDKROOT = iphoneos;
224 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
225 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
226 | TARGETED_DEVICE_FAMILY = "1,2";
227 | };
228 | name = Debug;
229 | };
230 | B1951C601F21EAFB001EDB92 /* Release */ = {
231 | isa = XCBuildConfiguration;
232 | buildSettings = {
233 | ALWAYS_SEARCH_USER_PATHS = NO;
234 | CLANG_ANALYZER_NONNULL = YES;
235 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
237 | CLANG_CXX_LIBRARY = "libc++";
238 | CLANG_ENABLE_MODULES = YES;
239 | CLANG_ENABLE_OBJC_ARC = YES;
240 | CLANG_WARN_BOOL_CONVERSION = YES;
241 | CLANG_WARN_CONSTANT_CONVERSION = YES;
242 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
243 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
244 | CLANG_WARN_EMPTY_BODY = YES;
245 | CLANG_WARN_ENUM_CONVERSION = YES;
246 | CLANG_WARN_INFINITE_RECURSION = YES;
247 | CLANG_WARN_INT_CONVERSION = YES;
248 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
249 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
250 | CLANG_WARN_UNREACHABLE_CODE = YES;
251 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
252 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
253 | COPY_PHASE_STRIP = NO;
254 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
255 | ENABLE_NS_ASSERTIONS = NO;
256 | ENABLE_STRICT_OBJC_MSGSEND = YES;
257 | GCC_C_LANGUAGE_STANDARD = gnu99;
258 | GCC_NO_COMMON_BLOCKS = YES;
259 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
260 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
261 | GCC_WARN_UNDECLARED_SELECTOR = YES;
262 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
263 | GCC_WARN_UNUSED_FUNCTION = YES;
264 | GCC_WARN_UNUSED_VARIABLE = YES;
265 | IPHONEOS_DEPLOYMENT_TARGET = 10.3;
266 | MTL_ENABLE_DEBUG_INFO = NO;
267 | SDKROOT = iphoneos;
268 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
269 | TARGETED_DEVICE_FAMILY = "1,2";
270 | VALIDATE_PRODUCT = YES;
271 | };
272 | name = Release;
273 | };
274 | B1951C621F21EAFB001EDB92 /* Debug */ = {
275 | isa = XCBuildConfiguration;
276 | buildSettings = {
277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
278 | DEVELOPMENT_TEAM = 49RE3G6CPG;
279 | INFOPLIST_FILE = SwiftMulticastProtocol/Info.plist;
280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
281 | PRODUCT_BUNDLE_IDENTIFIER = it.brokenice.SwiftMulticastProtocol;
282 | PRODUCT_NAME = "$(TARGET_NAME)";
283 | SWIFT_VERSION = 3.0;
284 | };
285 | name = Debug;
286 | };
287 | B1951C631F21EAFB001EDB92 /* Release */ = {
288 | isa = XCBuildConfiguration;
289 | buildSettings = {
290 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
291 | DEVELOPMENT_TEAM = 49RE3G6CPG;
292 | INFOPLIST_FILE = SwiftMulticastProtocol/Info.plist;
293 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
294 | PRODUCT_BUNDLE_IDENTIFIER = it.brokenice.SwiftMulticastProtocol;
295 | PRODUCT_NAME = "$(TARGET_NAME)";
296 | SWIFT_VERSION = 3.0;
297 | };
298 | name = Release;
299 | };
300 | /* End XCBuildConfiguration section */
301 |
302 | /* Begin XCConfigurationList section */
303 | B1951C481F21EAFB001EDB92 /* Build configuration list for PBXProject "SwiftMulticastProtocol" */ = {
304 | isa = XCConfigurationList;
305 | buildConfigurations = (
306 | B1951C5F1F21EAFB001EDB92 /* Debug */,
307 | B1951C601F21EAFB001EDB92 /* Release */,
308 | );
309 | defaultConfigurationIsVisible = 0;
310 | defaultConfigurationName = Release;
311 | };
312 | B1951C611F21EAFB001EDB92 /* Build configuration list for PBXNativeTarget "SwiftMulticastProtocol" */ = {
313 | isa = XCConfigurationList;
314 | buildConfigurations = (
315 | B1951C621F21EAFB001EDB92 /* Debug */,
316 | B1951C631F21EAFB001EDB92 /* Release */,
317 | );
318 | defaultConfigurationIsVisible = 0;
319 | };
320 | /* End XCConfigurationList section */
321 | };
322 | rootObject = B1951C451F21EAFB001EDB92 /* Project object */;
323 | }
324 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/project.xcworkspace/xcuserdata/frind.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol.xcodeproj/project.xcworkspace/xcuserdata/frind.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/xcuserdata/frind.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/xcuserdata/frind.xcuserdatad/xcschemes/SwiftMulticastProtocol.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
32 |
33 |
39 |
40 |
41 |
42 |
43 |
44 |
54 |
56 |
62 |
63 |
64 |
65 |
66 |
67 |
73 |
75 |
81 |
82 |
83 |
84 |
86 |
87 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol.xcodeproj/xcuserdata/frind.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | SwiftMulticastProtocol.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 | SuppressBuildableAutocreation
14 |
15 | B1951C4C1F21EAFB001EDB92
16 |
17 | primary
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // SwiftMulticastProtocol
4 | //
5 | // Created by Luca Becchetti on 21/07/17.
6 | // Copyright © 2017 Luca Becchetti. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "size" : "20x20",
5 | "idiom" : "iphone",
6 | "filename" : "iPhone_Notifications_iOS7-10_20pt@2x.png",
7 | "scale" : "2x"
8 | },
9 | {
10 | "size" : "20x20",
11 | "idiom" : "iphone",
12 | "filename" : "iPhone_Notifications_iOS7-10_20pt@3x.png",
13 | "scale" : "3x"
14 | },
15 | {
16 | "size" : "29x29",
17 | "idiom" : "iphone",
18 | "filename" : "iPhone_Settings_iOS5-10_29pt@2x.png",
19 | "scale" : "2x"
20 | },
21 | {
22 | "size" : "29x29",
23 | "idiom" : "iphone",
24 | "filename" : "iPhone_Settings_iOS5-10_29pt@3x.png",
25 | "scale" : "3x"
26 | },
27 | {
28 | "size" : "40x40",
29 | "idiom" : "iphone",
30 | "filename" : "iPhone_Spotlight_iOS7-10_40pt@2x.png",
31 | "scale" : "2x"
32 | },
33 | {
34 | "size" : "40x40",
35 | "idiom" : "iphone",
36 | "filename" : "iPhone_Spotlight_iOS7-10_40pt@3x.png",
37 | "scale" : "3x"
38 | },
39 | {
40 | "size" : "60x60",
41 | "idiom" : "iphone",
42 | "filename" : "iPhone_App_iOS7-10_60pt@2x.png",
43 | "scale" : "2x"
44 | },
45 | {
46 | "size" : "60x60",
47 | "idiom" : "iphone",
48 | "filename" : "iPhone_App_iOS7-10_60pt@3x.png",
49 | "scale" : "3x"
50 | },
51 | {
52 | "idiom" : "ipad",
53 | "size" : "20x20",
54 | "scale" : "1x"
55 | },
56 | {
57 | "idiom" : "ipad",
58 | "size" : "20x20",
59 | "scale" : "2x"
60 | },
61 | {
62 | "idiom" : "ipad",
63 | "size" : "29x29",
64 | "scale" : "1x"
65 | },
66 | {
67 | "idiom" : "ipad",
68 | "size" : "29x29",
69 | "scale" : "2x"
70 | },
71 | {
72 | "idiom" : "ipad",
73 | "size" : "40x40",
74 | "scale" : "1x"
75 | },
76 | {
77 | "idiom" : "ipad",
78 | "size" : "40x40",
79 | "scale" : "2x"
80 | },
81 | {
82 | "idiom" : "ipad",
83 | "size" : "76x76",
84 | "scale" : "1x"
85 | },
86 | {
87 | "idiom" : "ipad",
88 | "size" : "76x76",
89 | "scale" : "2x"
90 | },
91 | {
92 | "idiom" : "ipad",
93 | "size" : "83.5x83.5",
94 | "scale" : "2x"
95 | }
96 | ],
97 | "info" : {
98 | "version" : 1,
99 | "author" : "xcode"
100 | }
101 | }
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_App_iOS7-10_60pt@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_App_iOS7-10_60pt@2x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_App_iOS7-10_60pt@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_App_iOS7-10_60pt@3x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Notifications_iOS7-10_20pt@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Notifications_iOS7-10_20pt@2x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Notifications_iOS7-10_20pt@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Notifications_iOS7-10_20pt@3x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Settings_iOS5-10_29pt@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Settings_iOS5-10_29pt@2x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Settings_iOS5-10_29pt@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Settings_iOS5-10_29pt@3x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Spotlight_iOS7-10_40pt@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Spotlight_iOS7-10_40pt@2x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Spotlight_iOS7-10_40pt@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/AppIcon.appiconset/iPhone_Spotlight_iOS7-10_40pt@3x.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/first.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "first.pdf"
6 | }
7 | ],
8 | "info" : {
9 | "version" : 1,
10 | "author" : "xcode"
11 | }
12 | }
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/first.imageset/first.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/first.imageset/first.pdf
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/logo.imageset/28453311-1919a444-6df7-11e7-9ffd-e4438c01c903.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/logo.imageset/28453311-1919a444-6df7-11e7-9ffd-e4438c01c903.png
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/logo.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "scale" : "2x"
10 | },
11 | {
12 | "idiom" : "universal",
13 | "filename" : "28453311-1919a444-6df7-11e7-9ffd-e4438c01c903.png",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/second.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "second.pdf"
6 | }
7 | ],
8 | "info" : {
9 | "version" : 1,
10 | "author" : "xcode"
11 | }
12 | }
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Assets.xcassets/second.imageset/second.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lucabecchetti/SwiftMulticastProtocol/053eb2cecc916c36d55fe0c72bb364b32614faa2/SwiftMulticastProtocol/Assets.xcassets/second.imageset/second.pdf
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
31 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
112 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
175 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/FirstViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // FirstViewController.swift
3 | // SwiftMulticastProtocol
4 | //
5 | // Created by Luca Becchetti on 21/07/17.
6 | // Copyright © 2017 Luca Becchetti. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class FirstViewController: UIViewController,MessageDelegate {
12 |
13 | @IBOutlet weak var label : UILabel!
14 | @IBOutlet weak var textField : UITextField!
15 |
16 | override func viewDidLoad() {
17 | super.viewDidLoad()
18 | }
19 |
20 | override func viewWillAppear(_ animated: Bool) {
21 |
22 | //Register to delegate
23 | messageDelegateMulticast.addNotifier(notifier: self)
24 |
25 | if label.text == nil || label.text! == ""{
26 | label.textColor = UIColor.orange
27 | label.text = "Delegate has been subscribed"
28 | }
29 |
30 | }
31 |
32 | override func didReceiveMemoryWarning() {
33 | super.didReceiveMemoryWarning()
34 | // Dispose of any resources that can be recreated.
35 | }
36 |
37 |
38 | /// Function to send message
39 | ///
40 | /// - Parameter sender: UIButton
41 | @IBAction func sendMessage(_ sender: Any) {
42 |
43 | if let txt = textField.text{
44 | //Comunicate event to delegate
45 | messageDelegateMulticast.newMessageReceived(message: txt)
46 | }
47 | textField.text = ""
48 | textField.resignFirstResponder()
49 |
50 | }
51 |
52 | //MARK: MessageDelegate
53 |
54 |
55 | /// Called from multicast when new message has been received
56 | ///
57 | /// - Parameter message: custom type
58 | func newMessageReceived(message: ChatMessage) {
59 |
60 | label.textColor = UIColor.green
61 | label.text = "First class has received message: \(message)"
62 |
63 | }
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UIStatusBarTintParameters
32 |
33 | UINavigationBar
34 |
35 | Style
36 | UIBarStyleDefault
37 | Translucent
38 |
39 |
40 |
41 | UISupportedInterfaceOrientations
42 |
43 | UIInterfaceOrientationPortrait
44 |
45 | UISupportedInterfaceOrientations~ipad
46 |
47 | UIInterfaceOrientationPortrait
48 | UIInterfaceOrientationPortraitUpsideDown
49 | UIInterfaceOrientationLandscapeLeft
50 | UIInterfaceOrientationLandscapeRight
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/SecondViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // SecondViewController.swift
3 | // SwiftMulticastProtocol
4 | //
5 | // Created by Luca Becchetti on 21/07/17.
6 | // Copyright © 2017 Luca Becchetti. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class SecondViewController: UIViewController,MessageDelegate {
12 |
13 |
14 | @IBOutlet weak var label: UILabel!
15 |
16 | override func viewDidLoad() {
17 | super.viewDidLoad()
18 |
19 | }
20 |
21 | override func viewWillAppear(_ animated: Bool) {
22 |
23 | //Register to delegate
24 | messageDelegateMulticast.addNotifier(notifier: self)
25 |
26 | if label.text == nil || label.text! == ""{
27 | label.textColor = UIColor.orange
28 | label.text = "Delegate has been subscribed"
29 | }
30 |
31 | }
32 |
33 | override func didReceiveMemoryWarning() {
34 | super.didReceiveMemoryWarning()
35 | // Dispose of any resources that can be recreated.
36 | }
37 |
38 | //MARK: MessageDelegate
39 |
40 |
41 | /// Called from multicast when new message has been received
42 | ///
43 | /// - Parameter message: custom type
44 | func newMessageReceived(message: ChatMessage) {
45 |
46 | label.textColor = UIColor.green
47 | label.text = "Second class has received message: \(message)"
48 |
49 | }
50 |
51 |
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/SwiftMulticastProtocol.swift:
--------------------------------------------------------------------------------
1 | //
2 | // MessageDelegate.swift
3 | // SwiftMulticastProtocol
4 | //
5 | // Created by Luca Becchetti on 21/07/17.
6 | // Copyright © 2017 Luca Becchetti. All rights reserved.
7 | //
8 |
9 | import Foundation
10 |
11 | /// Custom type, edit as you need
12 | typealias ChatMessage = String
13 |
14 |
15 | /// Delegate to implement in all classes
16 | @objc protocol MessageDelegate {
17 |
18 | ///Called when new message has been received
19 | func newMessageReceived(message: ChatMessage)
20 |
21 | }
22 |
23 |
24 | /// Delegate multicast
25 | class MessageDelegateMulticast {
26 |
27 | //Array of registered classes
28 | private var notifiers: [MessageDelegate] = []
29 |
30 |
31 | /// New message received method
32 | ///
33 | /// - Parameter message: custom message type
34 | func newMessageReceived(message: ChatMessage){
35 | //Notify to all classes
36 | notifyAll { notifier in
37 | notifier.newMessageReceived(message: message)
38 | }
39 | }
40 |
41 |
42 | /// Method Register class from multicast, usually called in ViewDidLoad
43 | ///
44 | /// - Parameter notifier: class that implement MessageDelegate
45 | func addNotifier(notifier: MessageDelegate) {
46 | removeNotifier(notifier: notifier)
47 | notifiers.append(notifier)
48 | }
49 |
50 | /// Method to unregister class from multicast, usually called in ViewDidDisappear
51 | ///
52 | /// - Parameter notifier: class that implement MessageDelegate
53 | func removeNotifier(notifier: MessageDelegate) {
54 | for i in 0 ..< notifiers.count {
55 | if notifiers[i] === notifier || object_getClassName(notifiers[i]) == object_getClassName(notifier) {
56 | notifiers.remove(at: i)
57 | break;
58 | }
59 | }
60 | }
61 |
62 |
63 | /// Method that notify to all registered classes
64 | ///
65 | /// - Parameter notify: class that implement MessageDelegate
66 | private func notifyAll(notify: (MessageDelegate) -> ()) {
67 | for notifier in notifiers {
68 | notify(notifier)
69 | }
70 | }
71 |
72 | }
73 |
74 | /// Singleton class for multicast
75 | let messageDelegateMulticast = MessageDelegateMulticast()
76 |
--------------------------------------------------------------------------------
/SwiftMulticastProtocol/ThirdViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // SecondViewController.swift
3 | // SwiftMulticastProtocol
4 | //
5 | // Created by Luca Becchetti on 21/07/17.
6 | // Copyright © 2017 Luca Becchetti. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ThirdViewController: UIViewController,MessageDelegate {
12 |
13 | @IBOutlet weak var label: UILabel!
14 |
15 | override func viewDidLoad() {
16 | super.viewDidLoad()
17 |
18 | }
19 |
20 | override func viewWillAppear(_ animated: Bool) {
21 |
22 | //Register to delegate
23 | messageDelegateMulticast.addNotifier(notifier: self)
24 |
25 | if label.text == nil || label.text! == ""{
26 | label.textColor = UIColor.orange
27 | label.text = "Delegate has been subscribed"
28 | }
29 |
30 | }
31 |
32 | override func didReceiveMemoryWarning() {
33 | super.didReceiveMemoryWarning()
34 | // Dispose of any resources that can be recreated.
35 | }
36 |
37 | //MARK: MessageDelegate
38 |
39 |
40 | /// Called from multicast when new message has been received
41 | ///
42 | /// - Parameter message: custom type
43 | func newMessageReceived(message: ChatMessage) {
44 |
45 | label.textColor = UIColor.green
46 | label.text = "Third class has received message: \(message)"
47 |
48 | }
49 |
50 |
51 | }
52 |
53 |
--------------------------------------------------------------------------------