How to use global variable in Objective c without struct or enum - objective-c

var token : String { get and set}
Can any tell how to use the same thing in Objective C. In swift I can use any where in the project to access the token. I want the same thing to be done..

First, you should avoid to use global var with OOP.
If you really need to use global var, make it become an instance var in appdelegate.
If you don't want to put it in appdelegate, create a source file and declare as static var could solve it, remember access it by accessor, Never access global var directly.
GlobalVar.h
void setGlobalVar(int var);
int getGlobalVar(void);
GlobalVar.m
static int s_globalVar = 0;
void setGlobalVar(int var) {
s_globalVar = var;
}
int getGlobalVar(void) {
return s_globalVar;
}

Related

How would I implement this Objective-C Line in Swift?

In a library called UIScrollView-InfiniteScroll (linked here) you can find this line of code in Classes/UIScrollView+InfiniteScroll.m:
static const void *kPBInfiniteScrollStateKey = &kPBInfiniteScrollStateKey;
How do I rewrite this line in Swift? Is it even possible?
It would have helped a lot should have you included the original comment
// Keys for values in associated dictionary
static const void *kPBInfiniteScrollStateKey = &kPBInfiniteScrollStateKey;
You can use associated objects like this in Swift, courtesy of erhatsezer https://gist.github.com/serhatsezer/4abe382229b001b37b1ee2f46e62b90f#file-productdetail-extension-swift
private var associateKey: Void?
extension ProductDetail {
var totalSumOfBasket: String? {
get {
return objc_getAssociatedObject(self, &associateKey) as? String
}
set {
objc_setAssociatedObject(self, &associateKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}
Most basic way to do that its using https://swiftify.com tool. Easiest way to convert objc to swift.
Also for infinite scrollView best solution I found is https://github.com/rebeloper/DScrollview it's completely written in swift.

How to force invoke method with object type input value without any type casting in a series of overloaded methods?

For example I'm having a class with three overloaded methods like this:
class MyClass
{
int sum(int i)
{
// Method implementation.
}
int sum(string x)
{
// Method implementation.
}
int sum(object o)
{
// Method implementation.
}
}
My question is when I call the sum method of MyClass by passing any value (integer, string or object) it should invoke only third method (with object type input parameter)
class MainClass
{
static void Main(string[] args)
{
MyClass obj = new MyClass();
obj.sum(10);
obj.sum("X")
}
}
You said "without type casting" but you can't, because you need some way to indicate to the compiler which version to call, and the runtime uses the type it sees to do that bit. Boxing the int as an object means the compiler will pick the object version
sum(1);//call int version
sum((object)1); //call object version
sum((string)(object)"1"); //call string version
sum((object)(int)(object)1); //call object version
First of all, let me say that if you sometimes want to call one version of the sum function when working with ints and sometimes want to call another, overloading probably isn't the right tool to use. Overloading works best when you are implementing conceptually the same operation for a number of different types, and you want the compiler to figure out automatically which function is the right one to call for each type; if you need more manual control over which function is called, you're probably better off using different names.
That said, if you're sure that this is what you want to do, you could implement the overloaded version for object in terms of another function in the public interface, as in:
class MyClass
{
int sum(int i)
{
// Method implementation.
}
int sum(string x)
{
// Method implementation.
}
int sum(object o)
{
sum_object(o);
}
int sum_object(object o)
{
// Method implementation for objects
}
}
Then, when you want to apply the object version to int and string objects, you just call sum_object directly instead.

Objective-C Block in Swift - Variable Missing?

Here's a block type that I am defining in objective-c
typedef void (^arrayBlock)(NSArray *blockArray);
I have an objective-c class with a method that uses this as a return block
-(void)loadTimesWithSuccessBlock:(arrayBlock)successBlock;
When I try to use this method in Swift, this is what autocomplete gives me.
let timeClockLibrarian = HMFTimeClockLibrarian()
timeClockLibrarian.loadTimesWithSuccessBlock { ([AnyObject]!) -> Void in
//Where is blockArray?
}
I'm assuming that [AnyObject]! is supposed to be the NSArray. But I don't see how I'm supposed to get access to that variable?
If I were to use this method in Objective-C I get a result like this:
[timeClockLibrarian loadTimesWithSuccessBlock:^(NSArray *blockArray) {
//I can use the blockArray here :)
}];
[AnyObject]! is indeed only the type of the variable; autocomplete didn't name it. You just need to do something like (blockArray: [AnyObject]!).
let timeClockLibrarian = HMFTimeClockLibrarian()
timeClockLibrarian.loadTimesWithSuccessBlock { (blockArray: [AnyObject]!) -> Void in
// your code here
}
Write like this:
let timeClockLibrarian = HMFTimeClockLibrarian()
timeClockLibrarian.loadTimesWithSuccessBlock { blockArray in
doSomething(blockArray)
}
If you want to refer to weak self use this:
let timeClockLibrarian = HMFTimeClockLibrarian()
timeClockLibrarian.loadTimesWithSuccessBlock { [weak self] blockArray in
self?.doSomething(blockArray)
}
You may also want to get rid of implicit unwrapping. If so, specify nullability in Obj-C code:
typedef void (^arrayBlock)(nullable NSArray *blockArray);

How to change the value of a global variable in a function while there is a local variable of same name in C#

I want to change the global variable in a function where a local variable of same is already present.
int x=10; //global variable
void fun1()
{
fun2(5);
}
void fun2(int x)
{
x=7; //here i want that this statement assigns the value 7 to the global x
}
Just qualify it with this. It's a pretty common pattern, particularly for constructors:
public class Player
{
private readonly string name;
public Player(string name)
{
this.name = name;
}
}
While I view it as acceptable if your parameter really is meant to be a new value for the field (potentially in a method which creates a new instance based on the current one and the new value for the single field, for example), I would try to avoid it in general, just from a readability perspective. Of course, the names of your private fields are an implementation detail, but when reading the code for the method, it's confusing to have two different concepts represented by the same variable name.
Rename the local parameter value.
Like Yuriy Vikulov said.
this.x for non-static variables
int x=10; //global variable
void fun1()
{
fun2(5);
}
void fun2(int lx)
{
x=7; //if you want 7
x=lx; //if you want the paramValue
}
this.x for non-static classes
NameClass.x for static variables

c++/cli reference to property

Well, I haven't yet found something that says this is impossible, though I'm starting to think it might be. Can you make this work?
using namespace System;
template <typename T>
void unset(Nullable<T>& var) { var = Nullable<T>(); }
void unset(String^% var) { var=nullptr; }
//this is really a C# class in my situation, so I can't change its types
public ref class Foo
{
public:
property Nullable<Decimal> Dec;
property Nullable<int> Num;
property String^ Str;
};
int main()
{
Foo^ foo = gcnew Foo;
foo->Dec = Decimal(1.2);
foo->Num = 3;
foo->Str = "hi";
unset(foo->Dec);
unset(foo->Num);
unset(foo->Str);
Console::WriteLine(foo->Dec);
Console::WriteLine(foo->Num);
Console::WriteLine(foo->Str);
}
Update: unset is called from a code-generating macro which is called on about 50 params. I'd prefer not to have to go make varieties of the macro for each type.
It isn't possible. Setting a property requires calling the property setter function. There is no way to guess for the called method that it needs to call a function vs can assign the passed variable pointer. If you really want to do this then pass a delegate.
There is actually one .NET language that supports it, VB.NET generates code like this:
T temp = obj->prop;
func(temp)
obj->prop = temp;
There is however a dreadful aliasing problem with that, quite undebuggable. This goes belly up in the (rare) case where func() also uses the property. This is otherwise the way you'd work around the limitation, explicitly in your own code.
Beware that your code is wrong, possibly intentional, you are passing a C++ & reference, not a managed % interior pointer. The compiler is going to bitch about that, you can't create references or pointers to managed objects. They move. Unless the reference is to a variable on the stack. It doesn't otherwise change the answer.
For those who may end up here wondering how I got on with this, I ended up being lucky that the class I was working with was an LLBLGen Entity, so I was able to replace
unset(re->var);
with
{ SD::LLBLGen::Pro::ORMSupportClasses::IEntityField2^ f = re->Fields[#var]; \
if (f->IsNullable) \
f->CurrentValue = nullptr; }