Swift 4.2 - _Shared Attribute Near Type

NSMutableAttributedString: Ambiguous use of 'init(string:attributes:)'

The problem was in the init method declared in SwiftyAttributes 4.3.0:

extension NSAttributedString {
public convenience init(string str: String, attributes: [Attribute]) {
self.init(string: str, attributes: dictionary(from: attributes))
}
}

In SwiftyAttributes 5.0.0 this method was renamed to public convenience init(string str: String, swiftyAttributes attrs: [Attribute]). Therefore, I updated SwiftyAttributes to solve the problem. See this link for more detail about the fix added to SwiftyAttributes 5.0.0.

Swift 4.2 weird -.map- behavior

The way map works in Swift (and elsewhere) is that it can map (change) input from one type to another. So in your case, you will call map on your [CBPeripheral] and for each element, the map function will return another value. So, if the identifier is a String, you will end out with an array of Strings.

That is how it should work.

However :)

In your case, you have defined your mapping function like so

(perif) -> Void

Meaning that you will run through your array and call map, with a function that returns nothing. Meaning that the outcome will be an array filled with Void values.

Therefore, as others are suggesting in the comments, if you just want to examine the content of your cbPerifList, you are better of using a for in loop like so

for cbPerif in cbPerifList {
print(cbPerif.identifier)
}

If however, you're interested in getting an array of the names of your CBPeripherals you can do something like this

let names = cbPerifList.map { $0.identifier }

A comment to the above: To read more about how we can end up just writing { $0.name } have a look at the chapter about Closures in the Swift documentation, or this answer

Hope that helps you.

Swift 4.2 - Value of type '(____) - () - (____)' has no member 'childNode'

Initial values for let variables must be constants that can be initialized before the init() method is executed. That in turn means they can't reference self.

One workaround for that is making them lazy instance variables, which get initialized when they are first used (which, by definition, can't happen before init() has returned a reference to the object). In that case, the lazy variable would be initialized with the result from a closure.

Change:

let startGameButton:SKSpriteNode = self.childNode(withName: "SpriteName")

to

lazy var startGameButton:SKSpriteNode = self.childNode(withName: "SpriteName") as! SKSpriteNode

NSLayoutConstraint has no member 'Attribute'

It all depends on the version of Swift you are using.

NSLayoutConstraint.Attribute.height // Swift 4.2

NSLayoutAttribute.height // Swift 3.0+

NSLayoutAttributeHeight // Swift 2?

swift 4.2 crash on NSAttributedString.Key.writingDirection

The documentation for NSAttributedString.Key.writingDirection states:

The value of this attribute is an NSArray object containing NSNumber objects representing the nested levels of writing direction overrides, in order from outermost to innermost.

This attribute provides a means to override the default bidirectional text algorithm, equivalent to using the Unicode bidi control characters LRE, RLE, LRO, or RLO paired with PDF, but as a higher-level attribute. (See Unicode Standard Annex #9 for information about the Unicode bidi formatting codes.) The NSWritingDirectionAttributeName constant is a character-level attribute that provides a higher-level alternative to the inclusion of explicit bidirectional control characters in text. It is the NSAttributedString equivalent of the HTML markup using bdo element with the dir attribute.

The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of NSWritingDirection.leftToRight and NSWritingDirection.rightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Table 1.

At a minimum you need to provide an array:

let subAttr: [NSAttributedString.Key : Any] = [
.font : font,
.foregroundColor : color,
.writingDirection : [ NSNumber(value: NSWritingDirection.leftToRight.rawValue) ]
]

Swift 4.2 coalescing while downcasting multiple variables in a single if-let

You can try

 let prod = Product(foo:product["foo"] as? String ?? "Not Available" , bar: product["bar"] as? String ?? "Not Available" )

struct Root: Decodable {
let products: [Product]
}

struct Product: Decodable {
let foo: String
let bar: String?

enum CodingKeys: String, CodingKey {
case foo , bar
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)

do {
let foo = try container.decode(String.self, forKey: .foo)
let bar = try container.decodeIfPresent(String.self, forKey: .bar) ?? "Not Available"
self.init(foo:foo, bar:bar)

} catch let error {
print(error)
throw error
}
}
}


Related Topics



Leave a reply



Submit