No such module ‘Cocoa’ in Swift Playground

You are using an iOS playground (UIKit-based), not an OS X playground (Cocoa-based). Try creating a new playground and choosing “OS X” as the type instead of “iOS”. It should work fine after that. You can also change the type for an existing playground in the File Inspector (View→Inspectors→Show File Inspector) under Playground Settings→Platform. By … Read more

Could not find an overload for ‘/’ that accepts the supplied arguments

There are no such implicit conversions in Swift, so you’ll have to explicitly convert that yourself: average = Double(total) / Double(numbers.count) From The Swift Programming Language: “Values are never implicitly converted to another type.” (Section: A Swift Tour) But you’re now using Swift, not Objective-C, so try to think in a more functional oriented way. … Read more

Making my function calculate average of array Swift

You should use the reduce method to sum your sequence elements as follow: Xcode Xcode 10.2+ • Swift 5 or later extension Sequence where Element: AdditiveArithmetic { /// Returns the total sum of all elements in the sequence func sum() -> Element { reduce(.zero, +) } } extension Collection where Element: BinaryInteger { /// Returns … Read more

Can Swift playgrounds see other source files in the same project?

There’s two ways to use your project’s code in a Playground Playground’s Sources Folder Yes, in Xcode 6.3 Beta 3 (and hopefully, into the future): Playgrounds are now represented within Xcode as a bundle with a disclosure triangle that reveals Resources and Sources folders when clicked. These folders contain additional content that is easily accessible … Read more

tech