M_PI unresolved?? (iOS-only)

I've had this line of code in a personal project for a long time:


import Foundation


let π = CGFloat(M_PI)


Now suddenly, it's throwing an error:


/Users/knassar/dev/sotf/sotf/Classes/Geometry.swift:210:28: Use of unresolved identifier 'M_PI'


Running Xcode 7.3.1 (7D1014) & OSX 10.11.6 Beta (15G12a)

Is this a Swift/Xcode bug, or did something fundamental change with this update?

Replies

I can confirm this code works without error using Xcode 7.3.1 (7D1014), sam as yours, but under OS X 10.11.5. I'm not using the beta, so it seems they maybe did change something there. I assume you have tried the usual stuff since upgrading -- delete Derived Data folder, run the Option-Product>Clean Build Folder command, reboot? Or, it may be a phantom error caused in some surrounding code. I sometimes get these especially if I have mismatched braces or parentheses somewhere.

Try adding


import Cocoa

Thanks, but this file is used in an iOS app, so that won't work.


Some more information:

  • This file is shared between a Mac app and iOS app, and it only causes a build failure in the iOS app... maybe some issue with the iOS Foundation library?
  • This is definitely not a "bubble" from another bug... If I replace M_PI with 3.14159, the app compiles just fine.

So, try import UIKit

Yeah, neither helps.

I am using it in an iOS app, and not having any problems. There may be some problems with your old object files, have you tried Produc -> Clean Build Dir (Hold down alt) ?

Or delete derived data.

Yep, I've done all the usual suspects, Delete Derrived Data, full clean, even nuked my local repo and re-cloned.


What's really irritating is that Xcode happily auto-completes M_PI when I start to type it, but immediately declares it an error once I have.


I'm totally at a loss.



Edit:


Actually, I take it back... autocomplete no longer suggests it... I swear it did one time yesterday.

More weirdness:


When I switch to my OSX product (which builds fine), Cmd-Click on 'M_PI' takes me to the math.h header line 703:

#define M_PI        3.14159265358979323846264338327950288   / pi             */

exactly as you'd suspect.

If I switch to my iOS product, Cmd-Click on the same symbol in the same file gives me the system "funk" sound and the big "?" overlay of "Symbol not found"

What happens if you explicitly 'import Darwin' in your file? That's the framework it lives in, which should be included for you automatically.

`This may not be immediately helpful to you, but in Swift 3, you can use `CGFloat.pi`

Tried all the various imports, including Darwin. No change. Luckily, I have no deadline on this, so I'll probably just update to Swift 3 and use CGFloat.pi. In the meantime, pasting in the literal lets me work around this. Just find it bizarre that it's been working fine until now.

Just find it bizarre that it's been working fine until now.

It’s not just that it’s been working fine until now, it’s that it works fine for everyone else.

If you create a new project using the Single View Application template, does

M_PI
work there?

Share and Enjoy

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

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

One of the WWDC videos I *thought* said that M_PI was no longer supported - you have to use Float.pi instead. So that would be a Swift 2-3 difference.