Structure in a single Swift file? - swift

It might be basic and wrong but after understanding structures I can't understand where to practically put it.
Inside some class call it Main, I would like to encapsulate a set of variables for dimensions.
I know I can just do :
struct Dimensions{
var w:Int
var h:Int
}
class Main
{
//do things using the structure
}
But since i have a lot of variables and i want it clean , I would like to create a new Swift file and put it inside
So inside a file called Dimensions or else :
import Foundation
struct Dimensions{
var w:Int
var h:Int
}
then the structure is visible to anyone, without even using the Swift file name.
A few questions to ask :
It seems like a bad idea - why ?
How is it different from a Singeltone to share data between classes? (value type?)
What is the right place to put the structure outside the Main class to get some clear code ?
Should I make one file with many not related Structs ?

then the structure is visible to anyone
That is not true. Since your struct is not marked public, only code in your module can access it. Even if you write it in one single file, it is still accessible anywhere in your module.
without even using the Swift file name.
The reason why you are saying this might be because in other languages, you need to import a header file or something like that if you want to use something from another file (I'm not an expert in "other languages"). But Swift organises its code in units of modules, not files.
It seems like a bad idea - why ?
It is not a bad idea. Putting different types in different files is a good way to organise your code. When I go to Car.swift I wouldn't expect to see the class Game.
How is it different from a Singeltone to share data between classes? (value type?)
Here you are just writing things in different files. As far as the compiler is concerned, this is not much different from writing everything in a single file because Swift organises code in modules, not files. The Singleton pattern is something completely different. It is when you only have one shared instance of a type.
What is the right place to put the structure outside the Main class to get some clear code ?
In another file, because Main should really be in its own file.
Should I make one file with many not related Structs ?
No. That is a bad way of organising your code. When you want to find a particular struct, how do you know which file it is in?

Related

Generating Swift files from templates

My goal is to create (find if exist) a tool which can produce swift files from templates.
For example, let’s say I need to create new ViewController with UITableView. It should be based on MVVM architecture with dependency injection. Let’s name this View “PersonsList”.
So, for this task I need to produce:
PersonListViewController
PersonListViewModel
PersonListViewModelProtocol
PersonCell
VM for cell and protocol for VM
Lots of files.
I want to say to my tool something like that
create tableview-template Person
and as a result get generated files. Files should contain empty implementation of each classes.
How should I do that? I am thinking about simple console app but I don’t know which language I should use. Maybe there is a better idea? Maybe there is a ready tool? Any help? :)
You could manually create the templates yourself and then write a short script (in Python / bash / swift etc) that goes through and replaces keywords with arguments you've passed in.

Where to store extensions in a Swift project

For example, I want to use this extension:
import Foundation
extension String {
func exec (str: String) -> Array<String> {
....
}
}
Where should I save it? Should I create a new file extensions.swift?
For global extensions such as the one above I think it would be best to put it in an extensions.swift file. If your extending one of your own classes I find it best to keep it in the same file as the original class so that other developers know about it.
If you have multiple extensions for a particular global class such as String, you could group them into a StringExtensions.swift file.
I'd recommend keeping a group in the project and then creating Files called [Class]Extension. If you store all extension in the same file as mentioned in the other answers you might end up having a lot of issues finding the extension you are looking for and you end up with a file full of different responsibilities. In a small project that might not matter but its better to force good organisation early in a project because you never know which project might grow.
The old-way on objective-c was to name like:
UIView+FrameUtils.h
NSMutableArray+Sort.h
UIColor+HEX.h
NSDictionary+Nil.h
So it's clear what is extended (categorized in objective-c) and what new functional implemented. So you may use this style in Swift too

Parsing multiple XML web file iphone

Let's say i have a xml file with a tag named which contains the number of fields i want to show in my tableView and in another xml file i have the information to be displayed in that tableView.
The question is : Should i create 2 different file in my project (xmlparse1.h and .m + xmlparse2.h and .m) or should i just put all my code in 1 (xmlparse.h + .m) and differenciate which file i am parsing at the moment with a bool or something like that in the code?
I am developing an iphone app on Xcode 4.3 mac os x 10.7.4 if this might change
EDIT: 1st file :
<MenuPrin>
<humidite>82,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</humidite>
<tempmoy>
189,124,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700
</tempmoy>
</MenuPrin>
2nd File :
<Instal>
<nbrField>2</nbrField>
</Instal>
So it looks like this. So, since nbrField is 2 I would take the 2 first value from tempMoy and humidite and show them in a tableView.
END OF EDIT
If how you're parsing is similar in both instances, to add two sets of files that do effectively the same thing would be extremely inefficient and considered bad practice. It makes a lot more sense to create a parser that can handle the two different files than to write two parsers that can only handle a specific file each.
To more specifically answer your question, I would determine which file you are parsing and behave accordingly, whether you do this with a bool or something else is up to you.
EDIT: Here is the idea that just popped into my head, so if for whatever reason this wont work for you(Like I said its been a while), or someone has a better idea, I'm all ears. You could still have a set of files(.h and .m) that contain the definition of your parser. Your parser could contain within it a two variables of type Object1 and Object2 which are built in such a way that they resemble the data structure that you need to store what you parse once for your first type of file, and a different definition for your second type of file. This way when parsing once you determine which file it is you are reading, it just changes which of these two objects you write into. There are also plenty of variations for how you could set this up, and I can also think of a few cases for what you might be trying to do where this might not work, but there is the idea regardless.
If you think that the two sets of files approach is better for the application you are trying to write and makes more sense to you, given what has been discussed in the comments, it isn't necessarily a bad idea.
In case if u want to create two table view in one view u need to create a single .h & .m file...If u want to create individual table view u need to create two .h & .m...
Try TBXML parser...that is the easiest way and less memory usage for xmlparsing...

Are there special rules to follow when putting splitting coffeescript classes into separate files?

I'm trying to change some otherwise working code by pulling all the classes into separate files. This works for most classes, except for the part where it reads class window.Timeline. The error message reads ReferenceError: window is not defined
Any suggestions?
It sounds like your file containing that class isn't getting loaded into the window context. Is it possible that it got loaded in the context of another class? Could you post some cod examples in a jsFiddle?
The pattern that I usually follow when exporting coffeescript symbols to the parent context is
exports = exports ? this
class MyClass
someField: false
exports.MyClass = MyClass
If you are using a modern browser and know how to access the debugging console, you could put
console.log this
At the end of the file that is throwing the reference error. This will allow you to have a look to see what the this context is, which may help you troubleshoot.

Sinatra coffeescript --bare?

I've done some searching on this, but I cannot find info. I'm building an application inside sinatra, and using the coffeescript templating engine. By default the compiled code is wrapped as such:
(function() {
// code
}).call(this);
I'd like to remove that using the --bare flag, so different files can access classes and so forth that I'm defining. I realize that having it more contained helps against variable conflicts and so forth, but I'm working on two main pieces here. One is the business logic, and arrangement of data in class structures. The other is the view functionality using raphaeljs. I would prefer to keep these two pieces in separate files. Since the two files wrapped as such cannot access the data, it obviously won't work. However, if you can think of a better solution than using the --bare option, I'm all ears.
Bare compilation is simply a bad practice. Each file should export to the global scope only the public objects that matter to the rest of your app.
# foo.coffee
class Foo
constructor: (#abc) ->
privateVar = 123
window.Foo = Foo # export
Foo is now globally available. Now if that pattern isn't practical, maybe you should rethink your structure a bit. If you have to export too may things, you nest and namespace things better, so that more data can be exposed through fewer global variables.
I support Alex's answer, but if you absolutely must do this, I believe my answer to the same question for Rails 3.1 is applicable here as well: Put the line
Tilt::CoffeeScriptTemplate.default_bare = true
somewhere in your application.