Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Created August 25, 2020 13:19
Show Gist options
  • Save KalpeshTalkar/de566976a54823884f2329d399effe33 to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/de566976a54823884f2329d399effe33 to your computer and use it in GitHub Desktop.
A protocol that automatically adds an identifier on a class. Mostly useful in working with collection views and table views.
//
// Copyright © 2020 Kalpesh Talkar. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// For support: https://gist.github.com/KalpeshTalkar/de566976a54823884f2329d399effe33
//
import Foundation
/// Automatically add identifier to all the Swift classes.
/// Just inherit the class from this protocol and then `reuseIdentifier` static property will be accessible on the class that will return the class name in String.
public protocol ReuseIdentifying {
/// Returns the class name.
static var reuseIdentifier: String { get }
}
/// Extension for the actula implementation of the reuseIdentifier property.
extension ReuseIdentifying {
static var reuseIdentifier: String {
// This is the actual implementation of the static property.
// Return the class name in string format to identify the class.
return String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment