0

I have been trying to use shift operator and unwrapping in a piece of code, as I'm learning Swift (FYI just a beginner), so I'm trying out multiple things, but I'm stuck with this operator. Looking at different threads about shift operators, I came to a conclusion that,, I may have to overload my operands and then use them accordingly.

My piece of code :

var p : Int! 
p = 24 >> 2;
if let msg = p{ print (msg); } 
else { print("abcd"); }

Please, help! And also advice if my thinking is correct or not.

All I want to achieve is that both may work together.. If there is any way applicable... xD

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Why are you using `Int!` in the first place? What do you want to do and what problem are you facing? – Ricky Mo Feb 19 '19 at 06:24
  • @RickyMo I'm just trying out multiple things... As I said above... Since I'm a beginner to this language... I'm trying to find out multiple things, which may be of use to me in future. As per your questions... I want to use shift operator and unwrapping method(be it forced or implicit)...secondly.. The problem I'm facing is that.. The piece of code I'm trying gives error, "p = 24 >> 2; '>>' is unavailable." – Prannay Bothra Feb 19 '19 at 06:26
  • So what is you asking? – Ricky Mo Feb 19 '19 at 06:28
  • @RickyMo how can I use shift operator and optionals(with unwrapping feature) if possible... – Prannay Bothra Feb 19 '19 at 06:30
  • You should always unwrap any optional variables before using them. Your code didn't even involves an optional. An optional `Int` should be denoted by `Int?` not `Int!`. – Ricky Mo Feb 19 '19 at 06:30
  • @RickyMo i did used '!' so that compiler will implicitly unwrap it.. – Prannay Bothra Feb 19 '19 at 06:31
  • I really don't get your question. Can you provide an example that you want to do but didn't work? – Ricky Mo Feb 19 '19 at 06:32
  • @RickyMo var p : Int! p = 24 >> 2; if let msg = p{ print (msg); } else{ print("abcd"); } – Prannay Bothra Feb 19 '19 at 06:34
  • This line didn't work just because you are missing a `;` after `var p: Int!`, otherwise it has no problem in execution: `var p : Int!; p = 24 >> 2; if let msg = p{ print (msg); } else{ print("abcd"); } ` – Ricky Mo Feb 19 '19 at 06:35
  • @RickyMo it still says error. – Prannay Bothra Feb 19 '19 at 06:39
  • @PrannayBothra what error r u getting? – tabassum Feb 19 '19 at 06:45
  • @tabassum error : '>>' is unavailabe. P = 24 >> 2; – Prannay Bothra Feb 19 '19 at 06:49
  • What environment are you running on? Neither XCode nor http://online.swiftplayground.run/ give me such error. – Ricky Mo Feb 19 '19 at 06:51
  • @RickyMo there is an online playground...htpps: //iswift.org/playground – Prannay Bothra Feb 19 '19 at 07:03
  • That website does mention it uses a dev version of Swift4, which might not run properly. Please try another online playground. Or the best bet is using XCode if you have a mac. – Ricky Mo Feb 19 '19 at 07:08
  • @RickyMo so are you trying to say.. That my piece of code will work in swift5? Or do I need to do some changes...? – Prannay Bothra Feb 19 '19 at 07:24
  • Don't use `;`, this is Swift :) For me the code runs fine using an online playground and Swift 4.2 so not sure what this question is about – Joakim Danielson Feb 19 '19 at 07:25
  • @JoakimDanielson thnx... I also tried it in version 4.2 and it works... As you asked my question was... How can I use shift operators and optionals together... I'm a beginner to this language.. So trying to find out multiple things... While learning... – Prannay Bothra Feb 19 '19 at 07:30
  • It has nothing to do with the swift version, it's just that specific website (iswift.org//playground) has something wrong. – Ricky Mo Feb 19 '19 at 08:03
  • And for your learning, please don't write Swift in a single line with `;`. Also always avoid using `!`, especially while declaring your variables. A type `T!` means you are not sure it is optional or not, it can be used as both optional and non optional but risks throwing exception. These types are only used by generated codes (e.g. linking UI element from storyboard). For human written code, you must be knowing whether the variable you declare is optional (`T?`) or not (`T`). – Ricky Mo Feb 19 '19 at 08:08
  • @RickyMo thanks alot... – Prannay Bothra Feb 19 '19 at 08:18

1 Answers1

0

If I understand the question correctly this is a misunderstanding and has nothing to do specifically with using optionals and shift operators together

If you want to use the syntax if let x = y { then this is only allowed if y is declared as optional since the code is pointless if y is never nil because then you can safely use y directly.

When it comes to shift operators then neither value can be optional as far as I know so your example is a little contrived.

var y: Int?

if someCondition {
    y = 42
}

if let x = y {
   doStuff(x)
}  else {
    print("y was never set")
}

or unwrap with default value

var y: Int?

let x = (y ?? 0) + 42
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52