When I worked in Delphi, I had a special file with functions which were created by me. This file had a static way which was added to library path in Delphi settings and it could be used from anywere just by adding "lib" into "uses" block of .pas file.
Now I'm working in Swift and would like to make the same. I want to have a file which will have it's static way on PC and will be able to use itself from any ViewController just by adding "Import myLib"
How can I do it correctly?
Simple solution without an explicit static library:
Create a Swift file somewhere outside any product folder containing a class (or multiple classes) and class functions, for example
class GlobalFunctions {
class func function1()
{
println("function 1")
}
class func function2()
{
println("function 2")
}
}
In each project you need the file add the file (⌘⌥A)to the project but uncheck Copy items if needed in the dialog box.
Call a function with
GlobalFunctions.function1()
Any import statement or changing Build Settings is not needed
Related
I just started developing for iOS and WatchOS and have encountered a small issue that is probably very simple but I can't find any solution for it.
The issue I'm having is that I'm trying to structure my files in my own way but when creating new controllers, I cannot find them in the Custom Class dropdown on the Interface.storyboard
For example, I renamed InterfaceController.swift to Controllers/InitialViewController.swift to better match what I'm trying to do with my project structure.
For some reason InterfaceController still is showing up on there and if I type in the fullname of the new class, it won't show up. I'm unable to get that class here.
Any help at all would be great, thanks!
Renaming your file will not change the class name. You will have to modify the class declaration inside the file.
class InitialViewController: UIViewController {
/* code for your class */
}
Also, putting your files inside folders (blue folder icon) or Xcode groups (yellow folder icon) will not affect the class name.
Using swift I created a framework Common that contains functions and protocols I use repeatedly to cut down on code reuse.
Common.framework
public protocol CommonProtocol {}
I than created a framework that I want to share with others which includes some classes that extends CommonProtocol and passes CommonProtocol in response to some function calls.
Sharable.framework
public class Sharable : CommonProtocol {
func getCommon() -> CommonProtocol
}
Unfortunately when I attempt to use Sharable.framework in a Project I get there error:
Swift Compiler ErrorUse of undeclared type 'CommonProtocol'
Does anyone know how to make the protocol visible to Modules that use the Sharable.framework?
I am copying the frame Common.framework in the Copy Files step to Destination Frameworks (there was no noticeable change when I made it Shared Frameworks)
If possible I would prefer to only make certain protocols from Common.framework visible through Sharable.framework and I don't want to force my users to import multiple frameworks if I can avoid it.
I have a rather large Dart application that uses part and part of so that I don't have to use relative imports. However, the application may have something like this at the entry point file:
part 'file1.dart';
part 'file2.dart';
part 'file3.dart';
part 'some_sub_dir/file1.dart';
part 'some_sub_dir/file2.dart';
part 'some_sub_dir/file3.dart';
part 'some_sub_dir/file4.dart';
part 'some_sub_dir/file5.dart';
part 'some_sub_dir/file6.dart';
...
As far as I know, my IDE (WebStorm) doesn't automatically add a part statement when I create a new class. Is there any way I can avoid having to use so many parts? If not, can the process of adding new parts each time be automated?
I understand your problem as I can and I'll try to answer from my experience in Dart.
As I got it you are trying to keep all (or a lot) project files as parts of one library. It's wrong conception. In Dart library is not big thing. Here is few advices how to organize your files.
Don't try to keep a lot of files as parts of one library. Rather keep each file as separated library.
You can keep few classes in one file. But be sure classes works together for one idea.
You can split one library in few part files only if it has a lot of classes or one class is too big. But be sure all this files should be together in one library.
If you don't know how to combine classes in libraries keep each class in separated library. After days you will get understanding which classes plays together.
If one class from one library has usages in not only library but in other libraries - make it separated library.
Place parts in same folder with main library file.
If library has parts put all library files into separated folder.
Use relative links to parts.
Always use absolute links to other libraries. It will help you to make refactoring in future.
For example, this is a project like TODO list application. So, we have view class:
// This is a view html component class. //
library todolist.list_viewer;
import 'package:todolist/task.dart'; // model class
class ListViewer extends HtmlElement {
// it showes list of tasks
}
class TodoRenderer extends HtmlElement {
// this is a renderer for one todo item
}
And this is a model class:
// This is a todo model task //
library todolist.task;
class TodoProvider {
List<TodoItem> todos;
String addItem(TodoItem new Todo) {
//...
}
}
class TodoItem {
String Author;
DateTime date;
}
It looks simple now, divided into separated libraries. But if we want to add RecId class to keep todo database id it becomes too complex. We should split it in two libraries: todo_provider and todo_model and put RecId class into last one. Now it's good again.
If we wanna add one more model: a User, so each todo item may have executor or author. We can't just put it todo_model. Now we should to combine TodoItem and User classes into model library. So we just rename todo_model class to model and add User class into it.
Or instead last action we can make user_model library to keep User class. And as User model has recId property too we should extract RecId class into separated library.
It's all depends on how our classes big and complex.
I understand that:
part/part of is used to break a library into several parts (scripts). You have visibility to public and private members.
import is to "call/use" another library from your library. You have only visibility to public members of the imported library.
WebStorm can't infer you want a script to be part of your library.
I have create a UIViewController, e.g DemoTableViewController, and want to reuse it in other project. Instead of copying source file of controller, I need to create a library to reuse it, however, after I create a static library containing DemoTableViewController, and set the class of a view controller in storyboard, there's a an error:
Unknown class DemoTableViewController in Interface Builder file.
what can I do?
In the custom class field on the right top, you can see that you have given it as DemoTableViewController instead of DemoViewController(Assuming that what you have mentioned in question as DemoViewController is the class name you have given to this custom class). Change that and then check. It should work.
If the above is not working, make sure you have copied the DemoTableViewController.h file to your current project and this library is added to target settings -> Linked frameworks and libraries.
I am creating Class Files programmatically for UnitTests with NUnit.
I could already add them to TFS Source Control.
What I now need, is that I want to add them programmatically to the Project.
I have found several approaches, but I got none working... I don't want to edit the XMl-File manually.
Do you have some code snippets?
The naïve solution would be to edit the XML file manually. However, you don't have to do this.
You can use the Microsoft.Build.Evaluation.Project object model to manipulate Visual Studio project files. See this answer for more information on the differences between the Microsoft.Build.Evaluation and Microsoft.Build.Construction namespaces.
The following example opens YourProject.csproj and adds a new file called YourFile.cs as a Compile item type, then saves the project file back to disk.
using Microsoft.Build.Evaluation;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var project = new Project(#"YourProject.csproj");
project.AddItem("Compile", "YourFile.cs");
project.Save();
}
}
}