Swift playground File under Source not complete

Hello,

I created a playground, in this playground, I created a class, I instantiate it, everything is fine. I create the same class in a source swift file, make my class public but ... I do not have the foundation framework methods! for example, no ... uppercased! While I have these methods in the basic playground (I also use the framework foundation). What am I doing wrong? thanks a lot for your help


// Works with the Playground

import Foundation
import PlaygroundSupport

class me {

    var firstName: String
    var lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
   
    }

    func mergeUpperCase() -> String {
        return firstName.uppercased() + " " + lastName.uppercased()
   
    }
}

// Don't work with the Swift File under Sources

import Foundation

public class Person {
    public var firstName: String
    public var lastName: String

    public init(firName: String, lastName: String) {
        self.firstName = firName
        self.lastName = lastName
    
    }

    public func merge() -> String {
        return firstName + " " + lastName
    
    }

    public func mergeUpperCase() -> String {
        return firstName.xxxx + " " + lastName.xxxx // xxxx = no method uppercased()
    }

}

Replies

First up, if you format your code as code, using the

<>
icon, your posts will be easier to read.

Second, I’m assuming that the

xxxx
stuff at the end of your post is meant to be replaced by
uppercased()
. Please confirm.

Third, I put your

Person
class (with the previous fix) in a
Person.swift
file under
Sources
and it worked for me. The file compiled just fine and I was able to reference it from the a playground page like so:
let p = Person(firName: "f", lastName: "l")
p.mergeUpperCase()      // shows "F L"

This is starting from a blank macOS playground (File > New > Playground > macOS > Blank) with Xcode 9.2 on macOS 10.12.6. What type of playground did you start with?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Quinn “The Eskimo!”,

Thank you very much for your answer. I'm disappointed, what I want to do seems simple but it does not work ... not. I have Xcode Version 9.2 (9C40b) and macOS 10.12.6

Yes, xxxx should be represented by the "uppercased ()" method.

I start a Playground for macOS, blank. I select the "Sources" folder and create a new Swift file. I complete my code but I have no method of the framework "Foundation" (see image, send with a mail).

Thanks a lot for your help. It is very useful to me.

Best regards.

Olivier Dumont

For those reading along at home, Olivier sent me a copy of his playground via email and I ran that here in my office. It works as expected, just like the playground I created worked. Thus, there’s something specific to Olivier’s setup that’s triggering this problem.

I don’t have any really good advice on how to debug issues like this. There’s a standard sequence of things that I do to debug such user-level issues, including:

  1. Restarting

  2. Retrying from a freshly created user

  3. Retrying on a completely different Mac

These let you tease out the conditions that trigger the problem.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello, many thank you for the steps taken! So I deleted the Xcode preferences via the terminal => problem still present. I installed High Sierrea => not better ... I reinstalled Xcode => not better ... (sic!) I still have to try to recreate a new user account.

Hello, thank you for the steps taken! So I deleted the Xcode preferences via the terminal => problem still present. I installed High Sierrea => not better ... I reinstalled Xcode => not better ... (sic!) I still have to try to recreate a new User account!

Hello,
@eskimo I have the same problem in the latest Xcode version. I have tried all the steps you provided to see if it is user related.
Some I know also tried a completely different playground file on his Mac, without succes.

Do you maybe have some more ideas how I can fix this?

I have the same problem (Cannot find 'the_function_declared_in_sources' in scope), Xcode Version 14.3.1 (14E300c), Apple M1 Pro, macOS 13.4.1 (c) (22F770820d).