How to access swift class in other module of objective c - objective-c

How can i access that dummy class.swift in my sdksdk module and use it in ViewController.m file. any suggestion structure image
in dummy class there is a var name = "sid"
and i want to fetch that in my objective c class
dummy project link: https://drive.google.com/open?id=1PpJfdAlkT8kPhbxnFvJ2DyshvaD6K_mD

You need to have your sdkSDK module to be a framework, add it to you project. After that you can import sdkSDK in any of your Pracs classes or inject things to sdkSDK, best way I found to communicate from SDK to App is to use callbacks.
I did simple example in Swift but in your case there will be no difference if you will combine Swift and ObjC.
First - add SDK to Project.
Code inside SDK:
public class SDKDummyProvider {
public var sdkSurnameDidChanged: ((_ text: String) -> Void)?
var surname = "Siddhant" {
willSet {
// When you change surname its calls closure in `willSet`
sdkSurnameDidChanged?(newValue)
}
}
public init(name: String) {
print("Name from Pracs: \(name)")
}
}
Code inside Project:
import sdkSDK
class AppDummy {
var name = "Name"
let sdkProvider: SDKDummyProvider
init() {
// This is how you 'inject' name to SDK
sdkProvider = SDKDummyProvider(name: name)
// This is how SDK module will tell to Pracs that name has changed.
sdkProvider.sdkSurnameDidChanged = { newName in
print(newName)
}
}
}

Related

Passing native class pointer to another C++/CLI

I have two wrappers written in C++/CLI as followings.
One wrapper get a native class handle and it send this native handle to another class, however, I got a compile error.
I think there are some work-around,
1) #pragme make_public()
2) using IntPtr(sender) and static_cast with IntPtr.ToPointer(receiver).
What is the best solution?
namespace AWrapper {
public ref class AClass
{
public:
NativeClass* GetInfo() { return nativeClass; }
...
private:
NativeClass* nativeClass;
}
namespace BWrapper {
...
void ImageSensor::SetClass(AWrapper::AClass^ aclass)
{
NativeClass* native_tpr;
native_tpr = aclass->GetInfo(); // Not visible, like private
}
}

singleton object in react native

I'm new in react native.I want store multiple small small strings to common singleton object class and want to access it from singleton object for all component. Can anyone help me singleton object implementation for react native.
Ex
Component 1 -- Login button -- >> success --> need to store userID into singleton object.
Component 2 --> get stored userID from singleton object. How can i implement it.
Here is a simple way of doing it...
export default class CommonDataManager {
static myInstance = null;
_userID = "";
/**
* #returns {CommonDataManager}
*/
static getInstance() {
if (CommonDataManager.myInstance == null) {
CommonDataManager.myInstance = new CommonDataManager();
}
return this.myInstance;
}
getUserID() {
return this._userID;
}
setUserID(id) {
this._userID = id;
}
}
And here is how to use it...
import CommonDataManager from './CommonDataManager';
// When storing data.
let commonData = CommonDataManager.getInstance();
commonData.setUserID("User1");
// When retrieving stored data.
let commonData = CommonDataManager.getInstance();
let userId = commonData.getUserID();
console.log(userId);
Hope this works out for you :)
I suggest making a static class that stores data using AsyncStorage.
You mentioned in a comment that you are already using AsyncStorage, but don't like spreading this functionality throughout your app. (i.e. try-catches all over the place, each component needing to check if a key is available, etc.) If this functionality were in a single class, it would clean up your code a lot.
Another bonus to this approach is that you could swap out the implementation pretty easily, for example, you could choose to use an in-memory object or AsyncStorage or whatever and you would only have to change this one file
NOTE: AsyncStorage is not a safe way to store sensitive information. See this question for more info on the security of AsyncStorage and alternatives.
That said, this is how I imagine a global data holder class might look:
export default class dataManager {
static storeKeyValue(key, value) {
// your choice of implementation:
// check if key is used
// wrap in try-catch
// etc.
}
static getValueForKey(key) {
// get the value out for the given key
}
// etc...
}
Then to use this class anywhere in your app, just import wherever it's needed like so:
import dataManager from 'path/to/dataManager.js';
// store value
dataManager.storeKeyValue('myKey', 'myValue');
// get value
const storedValue = dataManager.getValueForKey('myKey');
EDIT: Using Flux, Redux, or a similar technology is probably the preferred/suggested way to do this in most cases, but if you feel the Singleton pattern works best for your app then this is a good way to go. See You Might Not Need Redux
There is a workaround for this, react native packager require all the modules in the compilation phase for a generating a bundle , and after first require it generates an internal id for the module, which is from then on referenced in the whole run-time memory , so if we export an instance of a class from the file, that object will be referenced every-time whenever that file is imported .
TLDR;
Solution I :
class abc {
}
module.exports = new abc()
Solution II : I assume you want to get your strings which are static and wont change , so you can declare them as static and access them directly with class name
FYI :this works with webpack also.
I might be too late for this, but I might as well share my own implementation based on Yeshan Jay's answer.
export default class Data {
static instance = null;
_state = {};
static get inst() {
if (Data.instance == null) {
Data.instance = new Data();
}
return this.instance;
}
static get state() {
return Data.inst._state;
}
static set state(state) {
Data.inst._state = state;
}
static setState(state) {
Data.inst._state = {...Data.inst._state, ...state}
}
}
And here's how you use it. It's pretty much mimicking React Component's state behavior, so you should feel at home with little to no adjustment, without the need to frequently modify the Singleton to add new properties now and then.
import Data from './Data'
// change the whole singleton data
Data.state = { userId: "11231244", accessToken: "fa7sd87a8sdf7as" }
// change only a property
Data.setState ({ userId: "1231234" })
// get a single property directly
console.log("User Id: ", Data.state.userId)
// get a single property or more via object deconstruction
const { userId, property } = Data.state
console.log("User Id: ", userId)
TS Class Example:
export class SingletonClass
{
private static _instance: SingletonClass;
public anyMetod(_value:any):any
{
return _value;
}
public static getInstance(): SingletonClass
{
if (SingletonClass._instance == null)
{
SingletonClass._instance = new SingletonClass();
}
return this._instance;
}
constructor()
{
if(SingletonClass._instance)
{
throw new Error("Error: Instantiation failed: Use SingletonClass.getInstance() instead of new.");
}
}
}
Use:
SingletonClass.getInstance().anyMetod(1);

Not able to use addElement in the flash-builder class

I am trying to experiment with graphics api in flash builder.
1) The default application is "Main.as" ( not Main.mxml)
2) The application uses Spark (Not the mx package)
What i am looking at is using the function addElement to show the shape in the following code
Here is the code :
package app
{
import flash.display.Shape;
import flash.display.Sprite;
import spark.core.SpriteVisualElement;
public class Main
{
public function Main()
{
var shape:Shape =new Shape() ;
shape.graphics.lineStyle(3,0xff);
shape.graphics.moveTo(0,0);
shape.graphics.lineTo(300,300);
var sve:SpriteVisualElement = new SpriteVisualElement() ;
sve.addChild(shape);
//***********************************
addElement( sve) ;// <<< Compiler error here
//***********************************
}
}
}
Your class must extend a class that supports visual elements.
In this case, you are attempting to extend Spark Application class:
package
{
import spark.components.Application;
public class Main extends Application
{
public function Main()
{
super();
}
}
}

TypeScript internal module usage

I have a class named BMW defined in BMW.ts as follows:
///<reference path="../Thing.ts"/>
module Entities.Cars {
import e = Entities;
export class BMW extends Vehicle {
public series: string;
constructor ( model : string, series : string) {
super("BMW", model)
this.series = series;
}
drive() {
alert("driving a bimmer is a different kind of feeling");
}
toString() : string
{
return this.getName() + " " + this.series + " " + this.getType();
}
}
}
In another file Thing.ts, I have Vehicle and Thing classes defined as follows:
module Entities {
// Class
export class Thing {
private _name: string;
private _type: string;
// Constructor
constructor (public name: string, public type: string) {
this._name = name;
this._type = type;
}
getName(): string { return this._name; }
setName(name: string) { this._name = name; }
getType(): string { return this._type; }
setType(name: string) {
this._type = name;
}
toString() : string
{
return "Entities.Thing";
}
}
export class Vehicle extends Thing {
public cargoCapacity: number;
public fuelType: string;
public owner: string;
constructor (make: string, model : string) {
super(make, model)
}
drive() {
}
toString(): string {
return "Entities.Vehicle";
}
}
}
When I attempt to execute the following code after referencing Thing and BMW TypeScript files:
var car = new Entities.Cars.BMW("335i", "E90");
car.drive();
I get an exception with the following error " Microsoft JScript runtime error: Unable to get value of the property 'BMW': object is null or undefined". The generated Javascript for BMW has an error. What is wrong with my above snippet?
There is nothing wrong with your code so it seems like your import order for the generated javascript files is wrong. The specification says the following:
Initialization order of the source files that make up the global module ultimately depends on the order in which the generated JavaScript files are loaded at run-time (which, for example, may be controlled by tags that reference the generated JavaScript files).
I have generated a file app.ts as follows:
///<reference path='Things.ts'/>
///<reference path='bmw/BMW.ts'/>
var car = new Entities.Cars.BMW("335i", "E90");
car.drive();
At this point you have two options:
Let the compiler determine the correct order for executing the files by generating a single output file
tsc --out app.js app.ts
Then you only have to source app.js.
Specify the correct order manually. For me, the following is the only order that works without throwing errors.
<html>
<head>
<script src="Things.js"></script>
<script src="bmw/BMW.js"></script>
<script src="app.js"></script>
</head>
<body>
</body>
</html>
Your code is fine.
My guess is that you are not placing script tags in your head element properly(wrong order, or ommitting some).
The simplest way to solve this, and to not have to remember about proper declaration sequence is to use single .js output file from tsc compiler by setting --out option.
EDIT: Depending on which js scenario you are working on(WSH, web app or some other js environment), you need to link js source files differently.
With wsh for instance, you could use FileSystemObject to read a source file, then evaluate it.
Or you could use AMDs...

Can not find model class in module application

I'm new in Zend, i had defined in my application.ini some lines to use multiple db.
resources.multidb.sitgm.adapter = "pdo_pgsql"
resources.multidb.sitgm.host = "localhost"
resources.multidb.sitgm.username = "postgres"
resources.multidb.sitgm.password = "pass"
resources.multidb.sitgm.dbname = "mydb"
resources.multidb.sitgm.isDefaultTableAdapter = true
In my APPLICATION Bootstrap i have a function:
public function _initDbRegistry()
{
$this->_application->bootstrap('multidb');
$multidb = $this->_application->getPluginResource('multidb');
Zend_Registry::set('db_sitgm', $multidb->getDb('sitgm'));
}
But when i had migrated to module squema, i have a default module, i added another DEFAULT Bootstrap.
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initDbRegistry()
{
//Do i must add something here to access application DB conf like app bootstrap????
}
}
In this point How i can call the application config beacuse i am getting an error in my default model class which can not find it.
class Default_Model_Base {
protected $db;
public $sql="";
function __construct() {
$this->db = Zend_Registry::get("db_sitgm"); //HERE I GOT THE ERROR
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
}
}
Thanks in advance
You don't have to define the _initDbRegistry in your module bootstrap as well. You can leave it in your application Bootstrap.php