Skip to content

Commit

Permalink
Improve MMMTypeName() and make it available for ObjC
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh committed Jul 1, 2023
1 parent 95cff73 commit c1f090b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
5 changes: 2 additions & 3 deletions MMMCommonCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMCommonCore"
s.version = "1.14.0"
s.version = "1.15.0"
s.summary = "Small bits and pieces reused in many pods from MMMTemple"
s.description = s.summary
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand All @@ -15,9 +15,8 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/mediamonks/#{s.name}.git", :tag => s.version.to_s }

s.ios.deployment_target = '11.0'
s.watchos.deployment_target = '2.0'
s.watchos.deployment_target = '4.0'
s.tvos.deployment_target = '9.0'
s.osx.deployment_target = '10.10'

s.subspec 'ObjC' do |ss|
ss.source_files = [ "Sources/#{s.name}ObjC/*.{h,m}" ]
Expand Down
47 changes: 37 additions & 10 deletions Sources/MMMCommonCore/CommonCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ extension NSErrorFriendly {
/// The name of the value's type suitable for logs or NSError domains: without the name of the module
/// and/or private contexts.
public func MMMTypeName(_ value: Any) -> String {

// To avoid "Something.Type" when the type is passed as value.
let name = type(of: value) is AnyClass
? String(reflecting: type(of: value))
: String(reflecting: value)
return name
.split(separator: ".")
// Skip "(unknown context at $...)" added for private types.
.filter { !($0.hasPrefix("(") && $0.hasSuffix(")")) }
// Ignore module names as it's the main module most of the time.
.dropFirst()
.joined(separator: ".")

var components = name.split(separator: ".")

// Skip "(unknown context at $...)" added for private types.
components = components.filter { !($0.hasPrefix("(") && $0.hasSuffix(")")) }

// Ignore module names, if any, as it's the main module most of the time.
if components.count > 1 {
components.removeFirst()
}

return components.joined(separator: ".")
}

extension NSError {
Expand Down Expand Up @@ -357,16 +363,16 @@ public func MMMBestMatchingLanguage(in languages: [String], preferredLanguages:
)
}

/// Objective-C bridge for MMMBestMatchingLanguage, since top-level functions are not supported.
/// Look at ``MMMBestMatchingLanguage(in:preferredLanguage:mode:)`` for more info.
@available(*, deprecated, message: "Use the corresponding function in `MMMCommonCoreHelpers`")
@objc public final class MMMBestMatching: NSObject {

/// See ``MMMBestMatchingLanguage(in:preferredLanguage:mode:)`` for more info.
@objc public class func language(
in languages: [String],
preferredLanguage: String,
mode: LanguageMatchingMode
) -> String? {
return MMMBestMatchingLanguage(in: languages, preferredLanguage: preferredLanguage, mode: mode)
MMMBestMatchingLanguage(in: languages, preferredLanguage: preferredLanguage, mode: mode)
}
}

Expand Down Expand Up @@ -429,3 +435,24 @@ extension Sequence {
return result
}
}

// MARK: -

/// This is to expose some of the functions back to ObjC.
@objc public class MMMCommonCoreHelpers: NSObject {

/// Obj-C alias for ``MMMTypeName``.
@objc public static func typeName(_ value: Any) -> String {
MMMTypeName(value)
}

/// Obj-C alias for ``MMMBestMatchingLanguage(in:preferredLanguage:mode:)``.
@objc public class func language(
in languages: [String],
preferredLanguage: String,
mode: LanguageMatchingMode
) -> String? {
MMMBestMatchingLanguage(in: languages, preferredLanguage: preferredLanguage, mode: mode)
}
}

0 comments on commit c1f090b

Please sign in to comment.