-3

I was playing around in Playground And I wondered Where is Main Function unlike in other languages , Swift does not have a main function , Whats the alternative or technique used to replace main function in swift when using in architecture outside that of Apple ( when used outside of ios and osx) since swift is soon going to be opensource ??

sriram hegde
  • 2,301
  • 5
  • 29
  • 43
  • 3
    possible duplicate of [Where does a Swift iOS application begin its life?](http://stackoverflow.com/questions/29722672/where-does-a-swift-ios-application-begin-its-life) While this question asks about iOS applications specifically, the answer addresses all Swift applications. – nhgrif Aug 26 '15 at 12:25
  • 1
    Its not duplicate because Im using swift as ios independant that is in playground , what you have suggested is ios dependant – sriram hegde Aug 26 '15 at 12:27
  • Read the answer, not the question. And if you wish to ask ***specifically*** about playground files only, I recommend making that question far more clear. – nhgrif Aug 26 '15 at 12:28
  • I have read the answer , its used in appDelegate , There is no AppDelegate in ios independant (in general) like if you use swift outside of ios architecture? and in playground the program runs even if you type a valid single line code , how is it doing without any main function – sriram hegde Aug 26 '15 at 12:30
  • There is an app delegate in OS X applications as well. – nhgrif Aug 26 '15 at 12:31
  • Swift is open source now , you can use swift outside of the Entire "Apple" family , there wont be AppDelegate in such case . – sriram hegde Aug 26 '15 at 12:32
  • 1
    It's not open source yet. It will eventually be open source. – nhgrif Aug 26 '15 at 12:33
  • Well if you want to take over the market , you should be the first to get the new product , in that way if its gunna eventually better start Adopting! – sriram hegde Aug 26 '15 at 12:35
  • That your question looks like a duplicate, but you insist it actually isn't, and have only used comments to clarify what you're actually asking means that the question itself is pretty unclear (and therefore, a bad question, but not quite off-topic maybe). – nhgrif Aug 26 '15 at 12:35
  • What more details you want ? – sriram hegde Aug 26 '15 at 12:35
  • 1
    The question is straight to the point , Where the Hell is Main function , if there is no main function , whats the alternate technology are they using , I searched the entire Docs of Apple no mention of it anywhere and you downVote saying its duplicate? ( I would have accepted the duplicate answer if swift was not going to be open source "Eventually") and hence this question needs upVote not downVote . – sriram hegde Aug 26 '15 at 12:38
  • It's unclear. You've made no effort to clearly distinguish your question from the duplicate it points to. Comments don't count. We're not up or downvoting any comments. Only the question is judged in voting. And no one can vote more than once (and you don't even know whether or not I even voted on it). At least two people think your question needs improvement. And there are no upvotes. – nhgrif Aug 26 '15 at 12:41
  • I have edited the question please check and stop downvoting unneccessarily , it feels bad when you contribute fairly and people down vote it – sriram hegde Aug 26 '15 at 12:43
  • 1
    Again, I only have maximum of one vote per question. If you don't want to feel bad about your question being downvoted, the solution absolutely is not to plead people not to downvote you. The solution is to write good, quality content. – nhgrif Aug 26 '15 at 12:43
  • But some people before even asking that this might be a possible duplicate they just become the "All Perfect People" and just down vote without properly Notifying the reason for that , and even when the reasons are provided they don't undo their down vote ,thats what feels bad and makes me wonder to type another good question if I ever encounter again. – sriram hegde Aug 26 '15 at 12:49
  • 2
    sriram hegde, I recommend you reevaluate your estimation of what makes a good question. If you're consistently getting downvoted or not many upvotes, there's not a clearer indication that you and the community don't agree on what makes for a quality question. That you've had 16 questions in less than 2 months (averaging two questions per week) in itself is a decent indicator that you might not be putting enough effort in your questions to warrant upvotes. There are over 10 million questions on SO. If you want your questions to be upvoted, model them after the ones that already are upvoted – nhgrif Aug 26 '15 at 12:55

1 Answers1

7

In a playground file, execution begins at the top of the playground file.

If you wish, you can imagine an implicit func main() { just above the first reachable line and a } just below the last reachable line. But that's probably not constructive. Instead, it's better to just realize that not all languages need a main function, and Swift is one of those languages.

For example, if we write a shell script, it will look something like this:

mkdir workspace
cd workspace
git clone https://github.com/nhgrif/SQLConnect

This is very simple... but there's no "main" function. Despite this, the terminal would be perfectly satisfied running this script, which would create a "workspace" directory and download my SQLConnect library into it.

A playground file is executed in much the same way as many interpreted languages. You do not need a main function. Single .swift files that are executed via the swift command in the terminal are executed in the exact same way. They start at the top of the file and work their way down.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Still not convinced , its assumption – sriram hegde Aug 26 '15 at 12:31
  • the answer is correct, also check the apple swift documentation if you are not convinced. From the "swift tour": print("Hello, world!") "..in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main() function." – Fred Aug 26 '15 at 12:50
  • 2
    ignore my previous comment: from what I understand, a playground does not need a main function, its just parsed and executed top to end. But when creating a console application you need to create a swift file called main.swift which acts like a main func. you can add println("hello world") in there. but when you create a different non-main swift file you'll get the error expressions not allowed on top level if you add the statement. – Fred Aug 26 '15 at 13:02