Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Heilum/ec459dabe96de13c453032fcd83e4dd5 to your computer and use it in GitHub Desktop.
Save Heilum/ec459dabe96de13c453032fcd83e4dd5 to your computer and use it in GitHub Desktop.
//
// Collection+SF.swift
// PanoAI
//
// Created by Forever Positive on 2021/1/30.
//
import Foundation
extension Array where Element == Optional<Any>{
var trimed:[Any] {
var newArray = [Any]()
for item in self{
if let item = item as? [Any?] {
newArray.append(contentsOf: item.trimed)
}else if item is NSNull || item == nil {
//skip
}else if let item = item{
newArray.append(item)
}
}
return newArray
}
}
extension Dictionary where Key == String, Value == Optional<Any> {
var trimed:[Key: Any] {
return self.compactMapValues({ v in
if let dic = v as? [String:Any?] {
return dic.trimed
}else if let _ = v as? NSNull {
return nil;
}else if let array = v as? [Any?] {
return array.trimed;
}else{
return v;
}
})
}
}
@Heilum
Copy link
Author

Heilum commented Jan 30, 2021

// MARK: - Usage
let dic:[String:Any?] = ["a":"a","b":nil,"c":NSNull(),"d":["1":"1","2":nil,"3":NSNull(),"4":["a","b",nil,NSNull()]],"e":["a","b",nil,NSNull()]]
print(dic.trimed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment