I'm trying to extend Swift's dictionary class in the following manner:
extension Dictionary {
func merge<K, V>(dict: [K:V]) -> Dictionary<K, V> {
var combinedDict: [K:V] = [:]
for (k, v) in self {
combinedDict[k] = v
}
for (k, v) in dict {
combinedDict[k] = v
}
return combinedDict
}
}
The first for loop gives me the error: "Cannot subscript a value of type '[K:V]' with an index of type 'Key'" but the second for loop is fine. I even commented out the first one to check and the second still works. Anyone know what the problem is? Thanks!
A dictionary's generic placeholder types are called Key and Value and you have to keep those names; you cannot arbitrarily rename them K and V.
Here's the implementation I use:
extension Dictionary {
mutating func addEntriesFromDictionary(d:[Key:Value]) { // generic types
for (k,v) in d {
self[k] = v
}
}
}
The dictionary type already defines Key and Value as generic variables, so K and V are not required (and cause the problem).
extension Dictionary {
func merge(dict: [Key : Value]) -> [Key : Value] {
var combinedDict = self
for (k, v) in dict {
combinedDict[k] = v
}
return combinedDict
}
}
How about this code.
extension Dictionary {
func merge(other: [Key: Value]) -> [Key: Value] {
var ret: [Key: Value] = self
for (key, value) in other {
ret[key] = value
}
return ret
}
}
Related
I'm trying to write a generic histogram function that operates on an Array, but I'm running into difficulties as Type 'Element' does not conform to protocol 'Hashable'.
extension Array {
func histogram() -> [Array.Element: Int] {
return self.reduce([Array.Element: Int]()) { (acc, key) in
let value = (acc[key] == nil) ? 1 : (acc[key]! + 1)
return acc.dictionaryByUpdatingKey(key: key, value: value)
}
}
}
where dictionaryByUpdatingKey(...) mutates an existing dictionary as follows:
extension Dictionary {
func dictionaryByUpdatingKey(key: Dictionary.Key, value: Dictionary.Value) -> Dictionary {
var mutableSelf = self
let _ = mutableSelf.updateValue(value, forKey: key)
return mutableSelf
}
}
I have tried replacing Array.Element with AnyHashable and then forcing the key as! AnyHashable, but this seems messy and the return type should preferably be of the same type as the Array.Element and not of AnyHashable.
I wish to use the Array extension as follows:
let names = ["Alex", "Alex", "James"]
print(names.histogram()) // ["James": 1, "Alex": 2]
or
let numbers = [2.0, 2.0, 3.0]
print(numbers.histogram()) // [3.0: 1, 2.0: 2]
Add the generic where clause: where Element: Hashable to your extension:
extension Sequence where Element: Hashable {
func histogram() -> [Element: Int] {
return self.reduce([Element: Int]()) { (acc, key) in
let value = acc[key, default: 0] + 1
return acc.dictionaryByUpdatingKey(key: key, value: value)
}
}
}
I also incorporated #MartinR's suggestion of using the new default value for dictionary look ups.
Using reduce(into:_:) you can do this much more simply and efficiently:
extension Sequence where Element: Hashable {
func histogram() -> [Element: Int] {
return self.reduce(into: [:]) { counts, elem in counts[elem, default: 0] += 1 }
}
}
First, you could limit the Element type to only be Hashable.
extension Array where Array.Element:Hashable {
After this, you might get another error because the swift compiler is a little "overstrained". Try to give him a hint:
typealias RT = [Array.Element: Int]
and use it everywhere. So:
extension Array where Array.Element:Hashable {
typealias RT = [Array.Element: Int]
func histogram() -> RT {
return self.reduce(RT()) { (acc, key) in
let value = (acc[key] == nil) ? 1 : (acc[key]! + 1)
return acc.dictionaryByUpdatingKey(key: key, value: value)
}
}
}
finally should work.
I have this code, but it shows error:
extension Collection {
func removingOptionals() -> [Element] {
var result = [Element](); // Error: cannot call value of non-function type '[Self.Element.Type]'
self.forEach({ (element) in if let el = element { result.append(el); } });
return result;
}
}
If I removed the (), the error becomes: Expected member name or constructor call after type name.
This code is supposed to transform [String?] into [String] by discarding all the null values. Or any other optional data types.
How can I do this?
You can use flatMap {} for this, instead of creation own function. Here is example of usage:
let strings: [String?] = ["One", nil, "Two"]
print(strings.flatMap { $0 })
And result will be ["One", "Two"]
You can continue to use the flatMap behavior of the Optional as the other answer shows, but it's going to be deprecated on the next Swift iteration.
If you want to add the extension to the collection type, you need to be a create a type to box the Optional (You can't extend Collection if the type is generic, like Optional).
protocol OptionalType {
associatedtype Wrapped
func map<U>(_ f: (Wrapped) throws -> U) rethrows -> U?
}
extension Optional: OptionalType {}
extension Collection where Iterator.Element: OptionalType {
func removeNils() -> [Iterator.Element.Wrapped] {
var result: [Iterator.Element.Wrapped] = []
result.reserveCapacity(Int(self.count))
for element in self {
if let element = element.map({ $0 }) {
result.append(element)
}
}
return result
}
}
I have an array consists of Dictionary.
I need to group them by Key in a dictionary.
I tried the line, but do not know what to write in handler. I am trying
globalArray.groupBy(handler: {$0["Name"]})
it gives error;
Cannot convert value of type "String?" to closure result type "_"
my group by extension is as follows;
extension Sequence {
// Using a `typealias` because it's shorter to write `E`
// Think of it as a shortcut
typealias E = Iterator.Element
// Declaring a `K` generic that we'll use as the type of the key
// for the resulting dictionary. The only restriction is having
// it conforming to the `Hashable` protocol
func groupBy<K: Hashable>(handler: (E) -> K) -> [K: [E]] {
// Creating the resulting dictionary
var grouped = [K: [E]]()
// Iterating over our elements
self.forEach { item in
// Retrieving the key based on the current item
let key = handler(item)
if grouped[key] == nil {
grouped[key] = []
}
grouped[key]?.append(item)
}
return grouped
}
}
Could you please show me the right usage?
BR,
Erdem
I am using this extension to group array , and it is working superbly
extension Array {
func grouped<T>(by criteria: (Element) -> T) -> [T: [Element]] {
var groups = [T: [Element]]()
for element in self {
let key = criteria(element)
if groups.keys.contains(key) == false {
groups[key] = [Element]()
}
groups[key]?.append(element)
}
return groups
}
}
How I use
array.grouped { (object:MyObjectClass) -> String in
return object.location?.name ?? "EmptyKey"
//Here you need to return your key
}
Hope it is helpful to you
I tried to create a custom iterator which returns wrapper abcContainer over raw data class abc
// raw data class
class abc {
var name : String = "";
init( _ value : String) {
name = value;
}
}
// with container, only "name" is to be visible
class abcContainer {
private var _abc : abc;
init( _ obj : abc) {
_abc = obj;
}
// + extra methods here
func getName() -> String {
return _abc.name
}
}
The point would be that the dictionary would return instances of abcContainer instead of just the plain raw abc class.
I wanted to use the sequence protocol to make the conversion automatic, but I was not able to transform the [String:abc] into [String:abcContainer] automatically like this:
// the iterator is implemented just iterating the inner basic dict
// but wrapping the result value as abcContainer
class abcIterator : Sequence, IteratorProtocol {
private var __source : [String:abc]?;
var index = 0
var myIterator : DictionaryIterator<String, abc>;
init(_ ctxArray: [String:abc]) {
self.__source = ctxArray
index = 0;
myIterator = (__source?.makeIterator())!
}
func next() -> abcContainer? {
let nextItem = myIterator.next();
if(nextItem != nil) {
return abcContainer((nextItem?.value)!);
}
return nil;
}
}
// this was supposed to be the wrapper over the collection
class abcCollection : Sequence {
private var __source : [String:abc]?;
init(_ list: [String:abc]) {
self.__source = list
}
func makeIterator() -> abcIterator {
return abcIterator(self.__source!);
}
}
I'm probably missing something very basic here. When I try to use the collection like this:
var dict : [String:abc] = [String:abc]();
dict["abba"] = abc("John Smith");
for (key,value) in abcCollection(dict) {
print(key, value.getName());
}
I get error: Expression type "abcCollection" is ambiguous without more context
Does anyone have idea how to make it work? What is missing? I have a feeling that this answer has the information I need...
Swift 2 to 3 Migration for Swift Sequence Protocol
The problem in your original code is that abcCollection(dict)
returned a sequence of abcContainer objects, and those cannot
be assigned to a (key, value) tuple.
You can achieve your goal with
class abcCollection : Sequence {
private var __source : [String:abc]
init(_ list: [String:abc]) {
self.__source = list
}
public func makeIterator() -> AnyIterator<(AnyObject,abcContainer)> {
let mapped = self.__source.lazy.map {
($0.key as AnyObject, abcContainer($0.value))
}
return AnyIterator(mapped.makeIterator())
}
}
Making __source non-optional makes all the (optional) unwrappings
redundant, and lazy.map { ... } returns a lazily evaluated
sequence of key/value pairs which is then type-erased.
Ok, perhaps the answer was abcIterator was not necessary, you could have defined the iterator directly just like done in the linked answer like this:
class abcCollection : Sequence {
private var __source : [String:abc]?;
init(_ list: [String:abc]) {
self.__source = list
}
public func makeIterator() -> AnyIterator<(AnyObject,abcContainer)> {
var it = self.__source?.makeIterator();
return AnyIterator {
let n = it?.next();
if n == nil { return nil }
return (n?.key as AnyObject, abcContainer((n?.value)!))
}
}
}
After that, the custom collection returned wrapped objects correctly.
I'm developing an iOS application in Swift.
When I updated the Xcode to 7.0, I'm getting error in swiftyJSON.
static func fromObject(object: AnyObject) -> JSONValue? {
switch object {
case let value as NSString:
return JSONValue.JSONString(value as String)
case let value as NSNumber:
return JSONValue.JSONNumber(value)
case let value as NSNull:
return JSONValue.JSONNull
case let value as NSDictionary:
var jsonObject: [String:JSONValue] = [:]
for (k:AnyObject, v:AnyObject) in value {// **THIS LINE- error: "Definition conflicts with previous value"**
if let k = k as? NSString {
if let v = JSONValue.fromObject(v) {
jsonObject[k] = v
} else {
return nil
}
}
}
What's the problem? Can you help, please?
for (k:AnyObject, v:AnyObject) in value { .. }
must be written in Swift 2 as
for (k, v) : (AnyObject, AnyObject) in value { .. }
From the Xcode 7 release notes:
Type annotations are no longer allowed in patterns and are considered
part of the outlying declaration. This means that code previously
written as:
var (a : Int, b : Float) = foo()
needs to be written as:
var (a,b) : (Int, Float) = foo()
if an explicit type annotation is needed. The former syntax was
ambiguous with tuple element labels.
But in your case the explicit annotation is actually not needed at all:
for (k, v) in value { .. }
because NSDictionary.Generator is already defined as a generator
returning (key: AnyObject, value: AnyObject) elements.