PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label cocoa. Show all posts
Showing posts with label cocoa. Show all posts

Sunday, September 18, 2022

[FIXED] How to print NSValue bytes

 September 18, 2022     cocoa, nsvalue, objective-c, printing, swift     No comments   

Issue

How does one print NSValue bytes? When using print it doesn't print all the bytes and puts '...'. E.g. {length = 128, bytes = 0x00000000 0000f03f 00000000 00000000 ... 00000000 0000f03f }

import Cocoa
import SceneKit

var identityMatrix = SCNMatrix4()

identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = 0
identityMatrix.m43 = 0
identityMatrix.m44 = 1

var a = [NSValue]()

a.append(NSValue(scnMatrix4:identityMatrix))

identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = -1.0
identityMatrix.m43 = 0
identityMatrix.m44 = 1

a.append(NSValue(scnMatrix4:identityMatrix))

print(a[0])

Solution

First of all, we can tidy this up by using an array literal, and the SCNMatrix4 initializer to get rid of all the mutability:

import Cocoa
import SceneKit

let a = [
    SCNMatrix4(
        m11: 1, m12: 0, m13: 0, m14: 0,
        m21: 0, m22: 0, m23: 0, m24: 0,
        m31: 0, m32: 0, m33: 1, m34: 0,
        m41: 0, m42: 0, m43: 0, m44: 1
    ),
    SCNMatrix4(
        m11: 1, m12:  0, m13: 0, m14: 0,
        m21: 0, m22:  1, m23: 0, m24: 0,
        m31: 0, m32:  0, m33: 1, m34: 0,
        m41: 0, m42: -1, m43: 0, m44: 1
    ),
].map(NSValue.init(scnMatrix4:))

print(a[0])

That makes the code much more sensible. We can also notice from the documentation that there's already a constant that we can use to get an identity matrix:

import Cocoa
import SceneKit

let a = [
    SCNMatrix4Identity,
    SCNMatrix4(
        m11: 1, m12:  0, m13: 0, m14: 0,
        m21: 0, m22:  1, m23: 0, m24: 0,
        m31: 0, m32:  0, m33: 1, m34: 0,
        m41: 0, m42: -1, m43: 0, m44: 1
    ),
].map(NSValue.init(scnMatrix4:))

print(a[0])

It's not clear why these matrices are wrapped in NSValue, but to display details about them (and call other methods on them), you'll need to fish the matrix back out of the NSValue box using the scnMatrix4Value API:

let sampleMatrix = a[0].scnMatrix4Value
print(sampleMatrix)

If you want to print the byte layout of these, (although it's still really not clear why you would want this, since AFAICT, NSValue and SCNMatrix4 make no promises about their memory layout), you can use the usual pointer APIs, in combination with some hex printing logic such as this:

withUnsafeBytes(of: &sampleMatrix) { sampleMatrixPointer in
    print(sampleMatrixPointer.map { String(format: "%02x", $0) }.joined(separator: ""))
}

prints:

000000000000f03f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f03f0000000000000000000000000000000000000000000000000000000000000000000000000000f03f


Answered By - Alexander
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, July 12, 2022

[FIXED] Which Cocoa view should I use for a list of messages?

 July 12, 2022     cocoa, message, nstableview     No comments   

Issue

I'd like to have a view that looks more or less like the message list in Tweetie (see screenshot) (no, it's not going to be a Twitter client, though it's similar ;). I'm not sure what Cocoa views should I use for that... I've mostly written stuff for the iPhone recently, and there's only one such control there (UITableView), but in AppKit there's several of them. Should I use NSTableView, or NSCollectionView, or is something else more suitable here?


Solution

Update: I'm going with SDListView - it's newer, seems to be maintained (it's a part of Steven Degutis's "TheGist" Twitter client), and the version used in that client looks almost identical to the one in Tweetie, which I kind of wanted to rip off anyway...

Update 2 (2014): Since 10.7 it's possible to use NSViews in NSTableView, so this whole answer is outdated. For any UITableView-like lists you should now just use view-based NSTableViews and you'll be fine.

--

Ok, I'll answer myself: according to this blog post, I could use NSTableView, but it's not a very good idea...:

For Mac, you have NSTableView, an antiquated slug of a component that uses NSCell objects instead of NSViews for various historical and performance-related reasons. NSCells are difficult to customize and cannot contain NSView objects (without jumping through hoops and introducing unnecessary complexity) which are the lifeblood of an interactive, engaging interface. Clickable hyperlinks inside of a span of text inside an NSCell? Good luck! Hover effects and Core Animation slickness? Yeah right! NSCell is like a mirage: it looks nice from afar but once you get up close and personal with it you wish you never saw it to begin with.

I think every native Twitter application for the Mac currently does something different for their timeline. Loren Brichter essentially wrote a UITableView port in order to make Tweetie's timeline and Steven Degutis has recently been working on an NSCollectionView-based timeline for his Twitter app. The new Echofon beta timeline is something different entirely with a completely custom text and layout manager that allows for hover effects on links as if it were a WebView. As for Beak I won't be getting into specifics in this entry but I'll just say that it's a totally custom NSScrollView with some fancy caching in the background.

I think I'll try to find some kind of custom open source control made specifically for that purpose... I'm considering AMCollectionView from http://www.harmless.de/cocoa-code.php and SDListView from http://github.com/sdegutis/SDListView.



Answered By - Kuba Suder
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing